Reputation: 2260
For example I have the URL:
http://www.website.com/example?something=abc&a=b
How can I get the something's content, "abc" and the content of a as strings?
Thanks in advance, Mateiaru
Upvotes: 1
Views: 92
Reputation: 2273
//var url = window.location.href;
var url = "http://www.website.com/example?something=abc&a=b";
var attributes = url.split("?")[1].split("&");
var keyValue = {};
for(i in attributes)
{
var pair = attributes[i].split("=");
keyValue[pair[0]] = pair[1];
}
alert(keyValue["something"]);
alert(keyValue["a"]);
Upvotes: 1
Reputation: 388316
var path = window.location.href;
var index = path.indexOf('?');
var paramstr = path.substring(index + 1);
var paramstrs = paramstr.split('&');
paramstrs.forEach(function(str, index){
var parts = str.split('=');
alert('value of ' + parts[0] + ' is ' + parts[1])
});
Upvotes: 2
Reputation: 949
Use a function helper:
function getURLParameter(name) {
return decodeURIComponent((new RegExp('[?|&]' + name + '=' + '([^&;]+?)(&|#|;|$)').exec(location.search)||[,""])[1].replace(/\+/g, '%20'))||null
}
Then call it like:
var aValue = getURLParameter(a);
Upvotes: 1