I've been looking for an efficient way to do this but haven't been able to find it, basically what I need is that given this url for example:

http://localhost/mysite/includes/phpThumb.php?src=http://media2.jupix.co.uk/v3/clients/4/properties/795/IMG_795_1_large.jpg&w=592&aoe=1&q=100

I'd like to be able to change the URL in the src parameter with another value using javascript or jquery, is this possible?

Thanks in advance.

share|improve this question

3 Answers

up vote 6 down vote accepted

Wouldn't this be a better solution?

var text = 'http://localhost/mysite/includes/phpThumb.php?src=http://media2.jupix.co.uk/v3/clients/4/properties/795/IMG_795_1_large.jpg&w=592&aoe=1&q=100';
var newSrc = 'www.google.com';
var newText = text.replace(/(src=).*?(&)/,'$1' + newSrc + '$2');

EDIT:

added some clarity in code and kept 'src' in the resulting link

share|improve this answer
great this worked perfectly! – javiervd Aug 24 '11 at 6:59
Doesn't handle the general case of replacing the last parameter. I changed the regex to /(src=).*?(&)?/ for a similar problem. – Bearddo Aug 14 '12 at 18:54
@ZenMaster Perfect :), can you explain meaning of text.replace(/(src=).*?(&)/,'$1' + newSrc + '$2'); – VaibhaV Feb 17 at 9:48
2 capturing groups... See more here: stackoverflow.com/questions/3512471/non-capturing-group – ZenMaster Feb 17 at 13:07

How about something like this:

<script>
function changeQueryVariable(keyString, replaceString) {
    var query = window.location.search.substring(1);
    var vars = query.split("&");
    for (var i = 0; i < vars.length; i++) {
        var pair = vars[i].split("=");
        if (pair[0] == keyString) {
            vars[i] = pair[0] + "=" replaceString
        }
    }
    return vars.join("&");
}
</script>
share|improve this answer
The only problem is that the URL doesn't come from the browser but from a variable, so I can't use the search method :( – javiervd Aug 24 '11 at 6:14
That is okay, you can do the same thing with regex. Here is an example: stackoverflow.com/questions/901115/… – Robert Martin Aug 24 '11 at 6:21

UpdatE: Make it into a nice function for you: http://jsfiddle.net/wesbos/KH25r/1/

function swapOutSource(url, newSource) {
    params = url.split('&');
    var src = params[0].split('=');
    params.shift();
    src[1] = newSource;
    var newUrl = ( src.join('=') + params.join('&')); 
    return newUrl; 
}

Then go at it!

var newUrl = swapOutSource("http://localhost/mysite/includes/phpThumb.php?src=http://media2.jupix.co.uk/v3/clients/4/properties/795/IMG_795_1_large.jpg&w=592&aoe=1&q=100","http://link/to/new.jpg");


console.log(newUrl);
share|improve this answer
The problem here is that you are always replacing the 1st parameter. So if I rearrange the string to be "localhost/mysite/includes/phpThumb.php?q=100&src=http://…; All the sudden it will break... – Robert Martin Aug 24 '11 at 6:25

Your Answer

 
or
required, but never shown
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.