How to add/update query parameters in URL without loading page Jquery/JavaScript

Sometimes we want to add or update the query parameters of a URL without loading the whole page because it saves our time. For example, Amazon when you filter it adds parameters to URL and loads the data without loading whole page.

JQuery/JavaScript has the ability to add or update the query parameters of a URL without loading page.

That’s really easy, here is the following code.

query = '?param1=value&param2=value2';
if (history.pushState) {
   var newurl = window.location.protocol + "//" + window.location.host + window.location.pathname + query;
   window.history.pushState({path:newurl},'',newurl);
}

Use this code and test.

2 Comments