简体   繁体   中英

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?

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM