Skip to content

Instantly share code, notes, and snippets.

@abhianair
Created April 21, 2020 10:09
Show Gist options
  • Select an option

  • Save abhianair/f1751e443f8642cea0769c4bcba8e4f4 to your computer and use it in GitHub Desktop.

Select an option

Save abhianair/f1751e443f8642cea0769c4bcba8e4f4 to your computer and use it in GitHub Desktop.
jquery url dynamic change
url = window.location.href;
paramName = 'myparam';
paramValue = $(this).val();
var pattern = new RegExp('('+paramName+'=).*?(&|$)')
var newUrl = url.replace(pattern,'$1' + paramValue + '$2');
var n=url.indexOf(paramName);
alert(n)
if(n == -1){
newUrl = newUrl + (newUrl.indexOf('?')>0 ? '&' : '?') + paramName + '=' + paramValue
}
window.history.pushState('title', active_tab, newUrl);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment