views:

56

answers:

3

How do I get a particular GET variable in JavaScript or jQuery?

I want to pass it on in ajax script in this sort of way:

$.ajax({
    url: 'foo/bar.php',
    data: { 
       search: $(this).val(),
       page: something //$_GET['page'] only in js
    },
    ...
A: 

what you try is almost correct, but you dont hav to label the data and you have a wron placed } in your code.

$.ajax({
    url: 'foo/bar.php',
    { 
       search: $(this).val(),
       page: 'something'
    },
    ...
});

for more information, take a look at the documentation.

EDIT: to read your get-variable, just do it like you always do: $s = $_GET['search'];. maybe you have to use $.get instead of $.ajax or set the request type for your $.ajax-call (don't know if it's POST by default, but you should be able to see this using firebug or something similar)

oezi
You can ignore that, that was basically just a type-o when I was providing an example of where I wanted to use it. The actual question is how to read the GET variable itself.
bcmcfc
A: 
var querystring = window.location.search;
Spudley