我有两个不同的查询字符串:?pid=
和 ?ref=
。目前,我使用 window.history.pushState
添加查询字符串。如何检查是否已经存在查询字符串以避免使用 ?pid=foo?ref=bar
而不是 ?pid=foo&ref=bar
?
当前代码:
if (!!$.cookie('myrefcookie')) {
var myref = $.cookie("myrefcookie")
var target = window.location.href + '?ref=' + myref;
window.history.pushState(null, null, target);
}
最佳答案
您可以查看window.location.search打个问号。
if (!!$.cookie('myrefcookie')) {
var myref = $.cookie("myrefcookie");
var query = window.location.search.indexOf('?') === -1 ? '?ref=' : '&ref=';
var target = window.location.href + query + myref;
window.history.pushState(null, null, target);
}
关于javascript - 如何检查查询字符串是否已存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40042464/