1

Possible Duplicate:
Get query string values in JavaScript
Parse query string in JavaScript
Storing php $_GET variable in a javascript variable?

How can I mimic $_GET in JavaScript, and display the value of the $_GET, so: how can I do this kind of thing in JavaScript:

if($_GET['id'] == 1)
{
    //Do something
}
Community
  • 1
  • 1
H Bellamy
  • 22,405
  • 23
  • 76
  • 114

3 Answers3

0

Could always use document.location.href to do this.

Something like:

var url = document.location.href, get;
get = url.split('?');
get = get.split('&');

Which will leave you with an array of the $_GET variables. Hope that helps.

k4t434sis
  • 519
  • 4
  • 17
0

There are two ways to achieve what you want:

  1. Parse the query string, as mentioned by knittl
  2. Store the $_GET contents in the user's session on the server side, and use an AJAX request to retrieve the data from the session.
Community
  • 1
  • 1
George Cummins
  • 28,485
  • 8
  • 71
  • 90
0

You need to get the URL parameters I'd suggest using a regular expression and/or jQuery, a quick example:

$.getUrlParameters = function(name, url) {
    var theURL = url || window.location.href;
    var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(theURL);
    if (!results) { return 0; }
    return results[1] || 0;
}

See this similar topic: How can I get query string values in JavaScript?

Community
  • 1
  • 1
bcmoney
  • 2,889
  • 1
  • 24
  • 30