I got this function from here in stack to replace url parameters like that :
function replaceUrlParam(paramName, paramValue){
var currentUrl = window.location.href;
var pattern = new RegExp('('+paramName+'=).*?(&|$)')
var newUrl = currentUrl.replace(pattern,'$1' + paramValue + '$2');
if(newUrl == currentUrl){
newUrl = newUrl + (newUrl.indexOf('?')>0 ? '&' : '?') + paramName + '=' + paramValue
}
window.history.pushState('',document.title,newUrl);
return newUrl;
}
www.mysite.com/index.php?id=14&&cat=20
and i use it like that:
replaceUrlParam('id', 15);
like that works fine.
But the problem is it duplicates the id if i use same id which is in the url like that.
replaceUrlParam('id', 14)--will give--->www.mysite.com/index.php?id=14&&cat=20&&id=14
How can i change this function to not give duplicates when its same id ? Thanks
fiddle here