3

Possible Duplicate:
How can I get query string values?

how can I get the HTTP GET request using javascript?

for example if I have access www.sample.com/div/a/dev.php?name=sample

how can I get the GET request of name=sample and the value if name which is sample?

4

4 Answers 4

3
Here is a fast way to get an object similar to the PHP $_GET array:

function get_query(){
    var url = location.href;
    var qs = url.substring(url.indexOf('?') + 1).split('&');
    for(var i = 0, result = {}; i < qs.length; i++){
        qs[i] = qs[i].split('=');
        result[qs[i][0]] = qs[i][1];
    }
    return result;
}
Usage:

var $_GET = get_query();
For the query string x=5&y&z=hello&x=6 this returns the object:

{
  x: "6",
  y: undefined,
  z: "hello"
}
Sign up to request clarification or add additional context in comments.

Comments

2

The window.location object might come useful here:

var parameter = window.location.search.replace( "?", "" ); // will return the GET parameter 

var values = parameter.split("=");

console.log(values); // will return and array as ["name", "sample"] 

3 Comments

will return a string of all query string parameters, anchors, and anything else after the question mark.
and you can split the string from '=', and you will get the parameter and the value
Did you test this? parameter = "foo=bar&blah=123", result ["foo", "bar&blah", "123"]
0

You can use location.href to fetch the full URL and then extract the values using split

Comments

0

Build the URL string and use jQuery get

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.