简体   繁体   中英

jquery modify url get parameters

I got a URL like this:

http://google.de/test.php?a=b&c=d&e=f

I know got to modify just one of the GET params like this: ( c is "h" instead of "d")

http://google.de/test.php?a=b&c=h&e=f

and redirect to the new url. All other GET params should stay the same.

How am I going to do this?

I agree its best to use a library for this purpose as mentioned in other answers.

However, here is a string replacement solution based on regular expressions if you need a simpler quick fix.

var url = "http://my.com/page?x=y&z=1&w=34";

var regEx = /([?&]z)=([^#&]*)/g;
var newurl = url.replace(regEx, '$1=newValue');

Here I'm replacing the query string key 'z' with 'newVlaue'

Have a look at this fiddle: http://jsfiddle.net/BuddhiP/PkNsx

as mentioned window.location.search will give you the query string with ? included.

I would then turn these into an object (perhaps with a library like http://github.com/medialize/URI.js ) to make it easier to work with the params. like var params = { key: value, key1: value1}

You could then modify the value you wanted and convert your key value pairs back to a query string.

After this you could use window.location to move the user to next page.

A little known feature of PHP is that a latter naming of an existing parameter overrides the earlier.

With this knowledge at hand, you can just do the following:

location.href = location.href + '&c=h';

Easy.

I've put together a really simple method but it will only work with what you're trying to achieve:

var url = 'http://google.de/test.php?a=b&c=d&e=f';
var gets =  url.split('.php?');
var ge = gets[1].split('&');
var change = ge[1].split('=');
var change[1] = 'h';
var newUrl = url[0] + '.php?' + ge[0] + '&' + change[0] + '=' + change[1] + '&' + ge[2];

You can try something like this:

var changed = "h"; // you can vary this GET parameter

var url = "http://google.de/test.php?a=b&e=f&c=" + changed; // append it to the end

window.location = newurl; // redirect the user to the url

Since your parameter can be at any place. So this will work fine

url="http://google.de/test.php?a=b&c=d&e=f";
url=url.replace(/&c=.*&/,"&c=h&")

Set or Update a URL/QueryString Parameter, and update URL using HTML history.replaceState()

You can try something like this:

var updateQueryStringParam = function (key, value) {
    var baseUrl = [location.protocol, '//', location.host, location.pathname].join(''),
        urlQueryString = document.location.search,
        newParam = key + '=' + value,
        params = '?' + newParam;

    // If the "search" string exists, then build params from it
    if (urlQueryString) {
        keyRegex = new RegExp('([\?&])' + key + '[^&]*');

        // If param exists already, update it
        if (urlQueryString.match(keyRegex) !== null) {
            params = urlQueryString.replace(keyRegex, "$1" + newParam);
        } else { // Otherwise, add it to end of query string
            params = urlQueryString + '&' + newParam;
        }
    }
    window.history.replaceState({}, "", baseUrl + params);
};

 location.href = location.origin+location.pathname+location.search; 

用你的新参数替换location.search,例如“?foo = bar&tic = tac”

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM