Jquery check if url has query string

If he visited the site for the first time and querysting was not added, or there is only one request attached to the "hos" URL, for example http: //hospitalsite/events/Pages/default.aspx? Hos = somevalue , then I need to apply if condition .. else condition works fine ... How to check if there is a request

$(".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
source share
4 answers

I think you need this value:

 var queryString = window.location.search; 

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

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

+20
source

Not completely sure about the entire query string, but it will help you check if the query string has separate variables:

 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"]); 

There are a few more answers to this question that may point you in the right direction:

How to get GET and POST variables using jQuery?

+2
source

try it

 $(".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(); } }); 
0
source

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.

0
source

Source: https://habr.com/ru/post/1391761/


All Articles