简体   繁体   中英

Why history.pushState not working?

I'm using the following code to change the URL of a page without reloading the whole page:

 alert( window.location.href ); 
 window.history.pushState( {} , 'foo', '/foo' );
 alert( window.location.href ); 

Result:

First alert :

file:///C:/Users/name/Desktop/html/first.html

Second alert :
No Alert and no change in URL but I was expecting following:

file:///C:/Users/name/Desktop/html/foo

Where's the glitch? And how to change the browser URL without reloading the page?

Don't run this command using the physical files. It should be hosted on a real domain or localhost.

Examples:

http://localhost/first.html

http://www.example.com/first.html

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