Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jquery check if URL has a querystring

Tags:

jquery

string

url

If its the first time visiting the site and no querysting has been added or if there is only one querystring attached to the url '?hos' like http://hospitalsite/events/Pages/default.aspx?hos=somevalue, then i need to apply the if condition..the else condition is working fine..How do I check to see if there is a query

$(".hospitalDropDown").change(function(e){
  currentURL=$(location).attr('href');
  if((currentURL == 'http://hospitalsite/events/Pages/default.aspx' or (....)) {
    window.location.href= 'http://hospitalsite/events/Pages/default.aspx'+'?hos='+$(this).val();
  } else {
    window.location.href = ( $(this).val() == "All Hospitals" ) ? 'http://hospitalsite/events/Pages/default.aspx': currentURL +'&hos='+ $(this).val(); 
  }
});
like image 947
Anju Thapa Avatar asked Jan 19 '12 04:01

Anju Thapa


2 Answers

I think you'd like this value:

var queryString = window.location.search;

If your URL was "http://www.google.com/search?q=findme", then the above queryString variable would be equal to "?q=findme".

You can check if that is non-empty to see if there is a query string or not.

like image 131
Cᴏʀʏ Avatar answered Oct 21 '22 20:10

Cᴏʀʏ


Not totally sure about the entire query string, but this will help you check if there are individual variables in the query string:

var $_GET = {};

document.location.search.replace(/\??(?:([^=]+)=([^&]*)&?)/g, function () {
    function decode(s) {
        return decodeURIComponent(s.split("+").join(" "));
    }

    $_GET[decode(arguments[1])] = decode(arguments[2]);
});

document.write($_GET["test"]);

This question has some more answers that might point you in the right direction:

how to get GET and POST variables with JQuery?

like image 40
HandiworkNYC.com Avatar answered Oct 21 '22 21:10

HandiworkNYC.com