Search code examples
javascriptjqueryhtml.htaccessmeta

How to change the page url without reloading the page


I want to know how can I change the page URL without reloading the page or changing the page content. I already use that code (JavaScript):

if(history.pushState){
  window.history.pushState("object or string", "Title", "/");
}else{
  document.location.href = "/";
}

But it doen't work in all browsers and Firefox marks it as insecure. Is there other ways to do what I want in JavaScript/jQuery/.htaccess/HTML meta tags

EDIT:
(1): I want an answer that doesn't use window.history.pushState
(2): Those answers don't help: how to chage url without redirect in javascript? , How do I modify the URL without reloading the page?


Solution

  • You can use HTML5 replaceState if you want to change the url but don't want to append this entry to the browser history:

    if(window.history.replaceState){
       window.history.replaceState(STATEDATA, TITLE, URL);
    }