-2

I have a variable set in my url:

http://mydomain.com?myvar=1

Now I need to check this when the page loads. I would normally do this in PHP but I need to use some jQuery. What I want to do is something like:

$(document).ready(function() {
    var somevar = $GET['myvar'];
    if (somevar = '1') {
        $('#someDiv').hide();
    }
});

How can I do this if its at all posible?

j08691
  • 197,815
  • 30
  • 248
  • 265
Satch3000
  • 44,076
  • 86
  • 209
  • 342
  • To do it client side in JS: http://stackoverflow.com/questions/901115/how-can-i-get-query-string-values To do it in PHP: http://stackoverflow.com/questions/4083900/getting-data-from-url-like-phps-get-for-jquery-ajax?rq=1 – John Carter Mar 06 '13 at 22:10

1 Answers1

2

Here's a function that will do that:

function getUrlVars() {
    var vars = {};
    var parts = window.location.href.replace(/[?&]+([^=&]+)=([^&]*)/gi, function(m,key,value) {
        vars[key] = value;
    });
    return vars;
}
MattDiamant
  • 8,010
  • 4
  • 35
  • 45