First, I believe that your URL is malformed, 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 go to Part 2, read this URL, so 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, it follows the list of all the 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: ""
I do not even have to make use of url.search
, you can even use the URLSearchParams
" to read the parameters, but it is very recent and so it is not supported by many browsers, in this case I advise you to use the following code (with similar and comparable structure, 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 complete 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 do not know what you're trying to do with the values in the queryString, but if you're thinking of creating a Route, I'd advise looking at the following library:
link