5

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(); 
  }
});

4 Answers 4

22

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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?

Comments

0

Try this

$(".hospitalDropDown").change(function(e){
        if(location.href.indexOf('?') == -1) {
            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': location.href +'&hos='+ $(this).val(); }

    });

Comments

0

Here is the javascript code to get the query string:

function getParameterByName(name) {
name = name.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
var regexS = "[\\?&]" + name + "=([^&#]*)";
var regex = new RegExp(regexS);
var results = regex.exec(window.location.href);
if (results == null)
    return "";
else
    return decodeURIComponent(results[1].replace(/\+/g, " "));
}

if this function returns null then there is no query string, you can directly call this function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.