Answer the question
In order to leave comments, you need to log in
Are there ready-made scripts for working with link parameters?
Hello. Is there a ready-made script with which you can add, remove and edit parameters in the link.
For example: site.com/?name=Ivan&surname=Ivanov
Here you need to do the above manipulations with "?name=Ivan&surname=Ivanov".
Answer the question
In order to leave comments, you need to log in
var queryDict = {}
location.search.substr(1).split("&").forEach(function(item) {
queryDict[item.split("=")[0]] = item.split("=")[1]
});
That's one way
function params(str) {
var res = {};
(str || location.search).toLowerCase().replace(/^\?/, "").split("&").forEach(function(str) {
str = str.split("=");
res[str[0]] = str[1] || true;
});
return res;
}
params("?name=Ivan&surname=Ivanov")
// Object {name: "Ivan", surname: "Ivanov"}
function query(obj) {
var str = "";
for (var prop in obj) {
str && (str += "&") || (str = "?");
if (obj.hasOwnProperty(prop)) str += prop + "=" + obj[prop];
}
return str;
}
query({name: "Ivan", surname: "Ivanov"})
// "?name=Ivan&surname=Ivanov"
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question