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?

Community
  • 1
  • 1
gadss
  • 21,687
  • 41
  • 104
  • 154
  • please clarify your question if possible – trebuchet Dec 07 '12 at 07:02
  • 1
    You mean something like this:: http://stackoverflow.com/questions/827368/use-the-get-parameter-of-the-url-in-javascript – Sudhir Bastakoti Dec 07 '12 at 07:04
  • Here you have a function that checks if vairable set in URL but it parse all the URI. You can use this function and update it for your needs. http://stackoverflow.com/questions/22491209/jquery-event-handler-if-get-is-set/22491912#22491912 – 4EACH Mar 19 '14 at 05:20
  • This question has also been asked here http://stackoverflow.com/questions/5448545/how-to-retrieve-get-parameters-from-javascript and here http://stackoverflow.com/questions/831030/how-to-get-get-request-parameters-in-javascript – Kirby Aug 07 '15 at 16:35

4 Answers4

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"
}
vaibhav
  • 129
  • 4
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"] 
Viren Rajput
  • 5,426
  • 5
  • 30
  • 41
0

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

Matanya
  • 6,233
  • 9
  • 47
  • 80
0

Build the URL string and use jQuery get

Toni Toni Chopper
  • 1,833
  • 2
  • 20
  • 29