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

3
  • document.URL or window.location You can get current url as string.You can manipulate it.Did you try anything? Commented Apr 20, 2013 at 11:29
  • possible duplicate of How to retrieve GET parameters from javascript? Commented Apr 20, 2013 at 11:30
  • snipplr.com/view/19838 - Google is your friend. Commented Apr 20, 2013 at 11:32

3 Answers 3

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])
});
Sign up to request clarification or add additional context in comments.

Comments

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"]);

Comments

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.