First, I believe your URL is misshapen, you should start your queryString with the character ?
, then we have the following URL:
http://www.meusite.com/?primeiro=11111&segundo=222222&terceiro=3333333333
now let’s the second part, read this URL, for such you can initialize a URL Object.
var urlAsString = "http://www.meusite.com/?primeiro=11111&segundo=222222&terceiro=3333333333";
var url = new URL(urlAsString);
If you make a console.log(json)
, you will see that it has some properties that you can use, follows the list of all properties and their values:
hash: ""
host: "www.meusite.com"
hostname: "www.meusite.com"
href: "http://www.meusite.com/?primeiro=11111&segundo=222222&terceiro=3333333333"
origin: "http://www.meusite.com"
password: ""
pathname: "/"
port: ""
protocol: "http:"
search: "?primeiro=11111&segundo=222222&terceiro=3333333333"
username: ""
need not even say that you will need to make use of the url.search
, you can even make use of the API URLSearchParams
to read the parameters, however it is very recent and therefore not supported by many browsers, in this case I advise you to use the following code (with similar structure and comparable, although not implement all methods).
var URLSearch = function (search) {
var that = this;
this.entries = {};
var start = search[0] == "?" ? 1 : 0;
var params = search.substring(start).split("&");
params.forEach(function (str) {
var array = str.split("=");
var prop = array[0];
var value = array[1];
that.entries[prop] = value;
});
}
URLSearch.prototype.get = function (prop) {
return this.entries[prop];
}
URLSearch.prototype.set = function (prop, value) {
this.entries[prop] = value;
}
URLSearch.prototype.has = function (prop, value) {
return this.entries[prop] ? true : false;
}
URLSearch.prototype.delete = function (prop, value) {
delete this.entries[prop];
}
URLSearch.prototype.toString = function (prop, value) {
var params = [];
for (var key in this.entries) {
params.push(key + "=" + this.entries[key]);
}
console.log();
return params.join("&");
}
var params = new URLSearch(url.search);
now to a full example:
var URLSearch = function (search) {
var that = this;
this.entries = {};
var start = search[0] == "?" ? 1 : 0;
var params = search.substring(start).split("&");
params.forEach(function (str) {
var array = str.split("=");
var prop = array[0];
var value = array[1];
that.entries[prop] = value;
});
}
URLSearch.prototype.get = function (prop) {
return this.entries[prop];
}
URLSearch.prototype.set = function (prop, value) {
this.entries[prop] = value;
}
URLSearch.prototype.has = function (prop, value) {
return this.entries[prop] ? true : false;
}
URLSearch.prototype.delete = function (prop, value) {
delete this.entries[prop];
}
URLSearch.prototype.toString = function (prop, value) {
var params = [];
for (var key in this.entries) {
params.push(key + "=" + this.entries[key]);
}
console.log();
return params.join("&");
}
// para pegar a URL atual, utilize `window.location.href`.
// var urlAsString = window.location.href;
var urlAsString = "http://www.meusite.com/?primeiro=11111&segundo=222222&terceiro=3333333333";
var url = new URL(urlAsString);
var params = new URLSearch(url.search);
console.log(params.entries); // Object {primeiro: "11111", segundo: "222222", terceiro: "3333333333"}
console.log(params.get("segundo")); // 222222
console.log(params.toString()); // primeiro=11111&segundo=222222&terceiro=3333333333
params.set("quarto", 44444);
console.log(params.toString()); // primeiro=11111&segundo=222222&terceiro=3333333333&quarto=44444
params.delete("terceiro");
console.log(params.toString()); // primeiro=11111&segundo=222222&quarto=44444
BONUS
I really don’t know what you are trying to do with the values of queryString, but if you are thinking of creating a Route, I advise you to look at the following blibioteca:
https://github.com/chrisdavies/rlite
Would that be something like? http://answall.com/questions/424/como-fa%C3%A7o-para-ler-values-da-url-in-javascript-querystring
– Marconi