Reputation: 257
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();
}
});
Upvotes: 5
Views: 17258
Reputation: 1127
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.
Upvotes: 0
Reputation: 11104
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?
Upvotes: 2
Reputation: 69905
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(); }
});
Upvotes: 0
Reputation: 107508
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.
Upvotes: 22