可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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?
回答1:
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
回答2:
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.
回答3:
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.
回答4:
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];
回答5:
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&")
回答6:
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
回答7:
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);
};
回答8:
location.href = location.origin+location.pathname+location.search;
Replace location.search by your new arguments like "?foo=bar&tic=tac"