Replace querystring value of url when changing dropdown menu

I have a dropdown that has a dual role. The user can go to the page ( http: //mysite/events/Pages/default.aspx ) directly and use the drop-down list, or they can first search and filter, the search of which was continued by further selecting the drop-down list. The first application URL will look like http: //mysite/events/Pages/default.aspx? Hos = Carmel and the second application URL http: //mysite/events/Pages/default.aspx? Kwd = health & type = Events & hos = Carmel This is what I am doing right now, but it behaves strangely and does it so that url http://mysite.events/Pages/default.aspx?hos=Crown&hos=Carmel

So, if the user selected carmel from the drop-down list for the first time and decided that he / she wants to search for Indianapolis, then he should either replace "Carmel" with indianapolis, or replace the whole line "& hos = Carmel" with "&, hos = Indianapolis "for the second case and"? Hos = Carmel "with"? Hos = Indianapolis "for the first scenario

$(".hospitalDropDown").change(function(e){ var querystring=window.location.search; var currentURL=$(location).attr('href'); if(location.href.indexOf('?') == -1) { window.location.href= 'http://mysite/events/Pages/default.aspx'+'?hos='+$(this).val(); } else{ window.location.href = ( $(this).val() == "All Hospitals" ) ? 'http://mysitesite/events/Pages/default.aspx': location.href +'&hos='+ $(this).val(); } )}; 

I found some great code that regex uses to process the request, but I don’t understand how the regex works. How can I use sth as shown below in my case?

 function replaceQueryString(url,param,value) { var re = new RegExp("([?|&])" + param + "=.*?(&|$)","i"); if (url.match(re)) return url.replace(re,'$1' + param + "=" + value + '$2'); else return url + '&' + param + "=" + value; } 
+6
source share
2 answers

You need to do something like this:

 function replaceQueryString(url,param,value) { var re = new RegExp("([?|&])" + param + "=.*?(&|$)","i"); if (url.match(re)) return url.replace(re,'$1' + param + "=" + value + '$2'); else return url + '&' + param + "=" + value; } $(".hospitalDropDown").change(function(e) { window.location.href = replaceQueryString(window.location.href, 'hos', $(this).val()); )}; 

The replaceQueryString you found has already been packaged into a function convenient for you, so you do not need to know anything about the regular expression in order to use it. All you have to do is pass this function to your URL, the parameter you are trying to change ("hos"), and the new value.

Also, $(location).attr('href') not really valid, and you didn't use it anyway, so just pull it out and stick to window.location.href .

Finally, although you do not need any knowledge of regular expressions in this particular case, you can certainly benefit from learning at least some basic regular expression syntax. Take a look at http://www.regular-expressions.info/ for some good explanations, and then use a regular expression tester like http://www.regextester.com/ to play around with samples and try to write some of your own.

+7
source
 $(".hospitalDropDown").change(function(e){ var newhos=($(this).val() == "All Hospitals" ) ? '' : '$1hos=' + $(this).val(); location.href=location.href.replace(/([\?\&])hos=\w+/,newhos); }); 
0
source

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


All Articles