B
B
Baxterok2015-02-07 00:03:00
JavaScript
Baxterok, 2015-02-07 00:03:00

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

3 answer(s)
A
Anton Tsepkovsky, 2015-02-07
@Zippovich

window.location - google

N
Nikita Baev, 2015-02-07
@drugoi

var queryDict = {}
location.search.substr(1).split("&").forEach(function(item) {
  queryDict[item.split("=")[0]] = item.split("=")[1]
});

S
Sergey Melnikov, 2015-02-07
@mlnkv

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"}

And so back
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 question

Ask a Question

731 491 924 answers to any question