The following example adds a query parameter to the URL without refreshing the page but only works on modern HTML5 browsers.
index.html
<!DOCTYPE html> <html> <head> <title>Add query parameter to the url without reload</title> </head> <body> <button onclick="updateURL();">Update</button> <script type="text/javascript"> function updateURL() { if (history.pushState) { var newurl = window.location.protocol + "//" + window.location.host + window.location.pathname + '?para=hello'; window.history.pushState({path:newurl},'',newurl); } } </script> </body> </html>
thanks i’m happy with you
LikeLike
Instead of ?para=hello how do I have javascript find search parameters that someone has typed in my website? so that when they hit the update button the url shows the search parameters they entered in?
LikeLike
Thanks. Good post.
LikeLike