Javascript 附加到 url 并刷新页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5997450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
append to url and refresh page
提问by amateur
I am looking to write a piece of javascript that will append a parameter to the current url and then refresh the page - how can I do this?
我想写一段 javascript 将一个参数附加到当前 url 然后刷新页面 - 我该怎么做?
回答by Shlomi Komemi
this should work (not tested!)
这应该有效(未测试!)
var url = window.location.href;
if (url.indexOf('?') > -1){
url += '¶m=1'
}else{
url += '?param=1'
}
window.location.href = url;
回答by Bo Frederiksen
Shorter than the accepted answer, doing the same, but keeping it simple:
比接受的答案更短,做同样的事情,但保持简单:
window.location.search += '¶m=42';
We don't have to alter the entire url, just the query string, known as the search attribute of location.
我们不必更改整个 url,只需更改查询字符串,称为位置的搜索属性。
When you are assigning a value to the search attribute, the question mark is automatically inserted by the browser and the page is reloaded.
当您为搜索属性赋值时,浏览器会自动插入问号并重新加载页面。
回答by yeyo
Most of the answers here suggest that one should append the parameter(s) to the URL, something like the following snippet or a similar variation:
这里的大多数答案都表明应该将参数附加到 URL,类似于以下代码段或类似的变体:
location.href = location.href + "¶meter=" + value;
This will work quite well for the majority of the cases.
对于大多数情况,这将非常有效。
However
然而
That's not the correct way to append a parameter to a URL in my opinion.
在我看来,这不是将参数附加到 URL 的正确方法。
Because the suggested approach does not test if the parameter is already set in the URL, if not careful one may end up with a very long URL with the same parameter repeated multiple times. ie:
因为建议的方法不会测试该参数是否已经在 URL 中设置,如果不小心,最终可能会得到一个很长的 URL,并且多次重复相同的参数。IE:
https://stackoverflow.com/?¶m=1¶m=1¶m=1¶m=1¶m=1¶m=1¶m=1¶m=1¶m=1
at this point is where problems begin. The suggested approach could and will create a very long URL after multiple page refreshes, thus making the URL invalid. Follow this link for more information about long URL What is the maximum length of a URL in different browsers?
此时是问题开始的地方。建议的方法可以并且会在多次页面刷新后创建一个很长的 URL,从而使 URL 无效。点击此链接了解有关长 URL 的更多信息不同浏览器中 URL 的最大长度是多少?
This is my suggested approach:
这是我建议的方法:
function URL_add_parameter(url, param, value){
var hash = {};
var parser = document.createElement('a');
parser.href = url;
var parameters = parser.search.split(/\?|&/);
for(var i=0; i < parameters.length; i++) {
if(!parameters[i])
continue;
var ary = parameters[i].split('=');
hash[ary[0]] = ary[1];
}
hash[param] = value;
var list = [];
Object.keys(hash).forEach(function (key) {
list.push(key + '=' + hash[key]);
});
parser.search = '?' + list.join('&');
return parser.href;
}
With this function one just will have to do the following:
使用此功能,只需执行以下操作:
location.href = URL_add_parameter(location.href, 'param', 'value');
回答by Paul Alexander
location.href = location.href + "¶meter=" + value;
回答by epascarello
function gotoItem( item ){
var url = window.location.href;
var separator = (url.indexOf('?') > -1) ? "&" : "?";
var qs = "item=" + encodeURIComponent(item);
window.location.href = url + separator + qs;
}
More compat version
更多兼容版本
function gotoItem( item ){
var url = window.location.href;
url += (url.indexOf('?') > -1)?"&":"?" + "item=" + encodeURIComponent(item);
window.location.href = url;
}
回答by Aftab
Please check the below code :
请检查以下代码:
/*Get current URL*/
var _url = location.href;
/*Check if the url already contains ?, if yes append the parameter, else add the parameter*/
_url = ( _url.indexOf('?') !== -1 ) ? _url+'¶m='+value : _url+'?param='+value;
/*reload the page */
window.location.href = _url;
回答by Gabrien
One small bug fix for @yeyo's thoughtful answer above.
上面@yeyo 深思熟虑的答案的一个小错误修复。
Change:
改变:
var parameters = parser.search.split(/\?|&/);
var parameters = parser.search.split(/\?|&/);
To:
到:
var parameters = parser.search.split(/\?|&/);
var parameters = parser.search.split(/\?|&/);
回答by Atchutha rama reddy Karri
Try this
尝试这个
var url = ApiUrl(`/customers`);
if(data){
url += '?search='+data;
}
else
{
url += `?page=${page}&per_page=${perpage}`;
}
console.log(url);
回答by estani
Also:
还:
window.location.href += (window.location.href.indexOf('?') > -1 ? '&' : '?') + 'param=1'
Just one liner of Shlomi answer usable in bookmarklets
仅可在书签中使用的一衬 Shlomi 答案