1

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

Chaoz
  • 2,119
  • 1
  • 20
  • 39
  • document.URL or window.location You can get current url as string.You can manipulate it.Did you try anything? – Shijin TR Apr 20 '13 at 11:29
  • possible duplicate of [How to retrieve GET parameters from javascript?](http://stackoverflow.com/questions/5448545/how-to-retrieve-get-parameters-from-javascript) – UltraInstinct Apr 20 '13 at 11:30
  • http://snipplr.com/view/19838/ - Google is your friend. – soyuka Apr 20 '13 at 11:32

3 Answers3

2
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])
});
Arun P Johny
  • 384,651
  • 66
  • 527
  • 531
1

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);
Rok Burgar
  • 949
  • 5
  • 7
1
//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"]);
MasNotsram
  • 2,105
  • 18
  • 28