如何从 url in javascript 中删除查询字符串数组
how to remove query string arrays from url in javascript
我在从 URL.
中删除查询字符串数组时遇到问题
这是我的 URL -
在Chrome中,它以给定的格式显示 -
Var url = "http://mywebsite.com/innovation?agenda%5B%5D=4995&agenda%5B%5D=4993#ideaResult";
然而,在 mozilla 中它以这种格式显示 -
http://mywebsite.com/innovation?agenda[]=4995&agenda[]=4993#ideaResult
我想删除特定的查询参数。
假设我想删除“4995”,那么最终 URL 应该是这样的-
http://mywebsite.com/innovation?agenda[]=4993#ideaResult
请帮忙
replace()函数呢?
var url = " http://mywebsite.com/innovation?agenda[]=4995&agenda[]=4993#ideaResult";
url = url.replace('agenda%5B%5D=4995&', '')
url = url.replace('agenda[]=4995&', '')
您可以解码 url 以使其一致
即。
var url = decodeURI("http://mywebsite.com/innovation?agenda%5B%5D=4995&agenda%5B%5D=4993#ideaResult");
...然后拆分字符串并删除查询字符串。这里有一个关于这个问题的例子 Remove querystring from URL
decodeURI()
函数根据Mozilla document:
Replaces each escape sequence in the encoded URI with the character
that it represents
所以你可以像下面这样使用它:
decodeURI(window.location).replace(/agenda\[\]=4995&?/,'')
function removeArrayParam(key, value, sourceURL) {
var rtn = sourceURL.split("?")[0],
param,
params_arr = [],
queryString = (sourceURL.indexOf("?") !== -1) ? sourceURL.split("?")[1] : "";
if (queryString !== "") {
params_arr = queryString.split("&");
for (var i = params_arr.length - 1; i >= 0; i -= 1) {
param = params_arr[i].split("[]=")[0];
paramValue = params_arr[i].split("[]=")[1];
if (param === key && paramValue === value) {
params_arr.splice(i, 1);
}
}
if(params_arr.length) {
rtn = rtn + "?" + params_arr.join("&");
}
}
return rtn;
}
这个函数会给你想要的结果。只需传递键、值和无哈希值 url.
var url = window.location.href;
var hash = window.location.hash;
var index_of_hash = url.indexOf(hash) || url.length;
var hashless_url = url.substr(0, index_of_hash);
var desired_url = removeArrayParam(key, value, unescape(hashless_url));
一直在寻找 non-plugin 从查询字符串中删除数组和 non-arrays 的解决方案,然后是 re-adding。它可能会更优雅一些,但这对我的所有测试用例都非常有效。
function removeURLParameter(param, url) {
url = decodeURI(url).split("?");
path = url.length == 1 ? "" : url[1];
path = path.replace(new RegExp("&?"+param+"\[\d*\]=[\w]+", "g"), "");
path = path.replace(new RegExp("&?"+param+"=[\w]+", "g"), "");
path = path.replace(/^&/, "");
return url[0] + (path.length
? "?" + path
: "");
}
function addURLParameter(param, val, url) {
if(typeof val === "object") {
// recursively add in array structure
if(val.length) {
return addURLParameter(
param + "[]",
val.splice(-1, 1)[0],
addURLParameter(param, val, url)
)
} else {
return url;
}
} else {
url = decodeURI(url).split("?");
path = url.length == 1 ? "" : url[1];
path += path.length
? "&"
: "";
path += decodeURI(param + "=" + val);
return url[0] + "?" + path;
}
}
使用方法:
url = location.href;
-> http://example.com/?tags[]=single&tags[]=promo&sold=1
url = removeURLParameter("sold", url)
-> http://example.com/?tags[]=single&tags[]=promo
url = removeURLParameter("tags", url)
-> http://example.com/
url = addURLParameter("tags", ["single", "promo"], url)
-> http://example.com/?tags[]=single&tags[]=promo
url = addURLParameter("sold", 1, url)
-> http://example.com/?tags[]=single&tags[]=promo&sold=1
当然,要更新参数,只需删除然后添加。随意为它制作一个虚拟函数。
我在从 URL.
中删除查询字符串数组时遇到问题这是我的 URL -
在Chrome中,它以给定的格式显示 -
Var url = "http://mywebsite.com/innovation?agenda%5B%5D=4995&agenda%5B%5D=4993#ideaResult";
然而,在 mozilla 中它以这种格式显示 -
http://mywebsite.com/innovation?agenda[]=4995&agenda[]=4993#ideaResult
我想删除特定的查询参数。
假设我想删除“4995”,那么最终 URL 应该是这样的-
http://mywebsite.com/innovation?agenda[]=4993#ideaResult
请帮忙
replace()函数呢?
var url = " http://mywebsite.com/innovation?agenda[]=4995&agenda[]=4993#ideaResult";
url = url.replace('agenda%5B%5D=4995&', '')
url = url.replace('agenda[]=4995&', '')
您可以解码 url 以使其一致
即。
var url = decodeURI("http://mywebsite.com/innovation?agenda%5B%5D=4995&agenda%5B%5D=4993#ideaResult");
...然后拆分字符串并删除查询字符串。这里有一个关于这个问题的例子 Remove querystring from URL
decodeURI()
函数根据Mozilla document:
Replaces each escape sequence in the encoded URI with the character that it represents
所以你可以像下面这样使用它:
decodeURI(window.location).replace(/agenda\[\]=4995&?/,'')
function removeArrayParam(key, value, sourceURL) {
var rtn = sourceURL.split("?")[0],
param,
params_arr = [],
queryString = (sourceURL.indexOf("?") !== -1) ? sourceURL.split("?")[1] : "";
if (queryString !== "") {
params_arr = queryString.split("&");
for (var i = params_arr.length - 1; i >= 0; i -= 1) {
param = params_arr[i].split("[]=")[0];
paramValue = params_arr[i].split("[]=")[1];
if (param === key && paramValue === value) {
params_arr.splice(i, 1);
}
}
if(params_arr.length) {
rtn = rtn + "?" + params_arr.join("&");
}
}
return rtn;
}
这个函数会给你想要的结果。只需传递键、值和无哈希值 url.
var url = window.location.href;
var hash = window.location.hash;
var index_of_hash = url.indexOf(hash) || url.length;
var hashless_url = url.substr(0, index_of_hash);
var desired_url = removeArrayParam(key, value, unescape(hashless_url));
一直在寻找 non-plugin 从查询字符串中删除数组和 non-arrays 的解决方案,然后是 re-adding。它可能会更优雅一些,但这对我的所有测试用例都非常有效。
function removeURLParameter(param, url) {
url = decodeURI(url).split("?");
path = url.length == 1 ? "" : url[1];
path = path.replace(new RegExp("&?"+param+"\[\d*\]=[\w]+", "g"), "");
path = path.replace(new RegExp("&?"+param+"=[\w]+", "g"), "");
path = path.replace(/^&/, "");
return url[0] + (path.length
? "?" + path
: "");
}
function addURLParameter(param, val, url) {
if(typeof val === "object") {
// recursively add in array structure
if(val.length) {
return addURLParameter(
param + "[]",
val.splice(-1, 1)[0],
addURLParameter(param, val, url)
)
} else {
return url;
}
} else {
url = decodeURI(url).split("?");
path = url.length == 1 ? "" : url[1];
path += path.length
? "&"
: "";
path += decodeURI(param + "=" + val);
return url[0] + "?" + path;
}
}
使用方法:
url = location.href;
-> http://example.com/?tags[]=single&tags[]=promo&sold=1
url = removeURLParameter("sold", url)
-> http://example.com/?tags[]=single&tags[]=promo
url = removeURLParameter("tags", url)
-> http://example.com/
url = addURLParameter("tags", ["single", "promo"], url)
-> http://example.com/?tags[]=single&tags[]=promo
url = addURLParameter("sold", 1, url)
-> http://example.com/?tags[]=single&tags[]=promo&sold=1
当然,要更新参数,只需删除然后添加。随意为它制作一个虚拟函数。