jquery modify url get parameters

2020-06-20 19:17发布

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?

8条回答
狗以群分
2楼-- · 2020-06-20 19:51

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
查看更多
兄弟一词,经得起流年.
3楼-- · 2020-06-20 19:56

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.

查看更多
登录 后发表回答