How to replace url parameter with javascript/jquery?

前端 未结 17 2325
旧时难觅i
旧时难觅i 2020-11-28 04:06

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://localh         


        
17条回答
  •  春和景丽
    2020-11-28 04:22

    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);
    

提交回复
热议问题