how to fully change url without reloading the page to new url?

前端 未结 2 984
后悔当初
后悔当初 2021-01-29 12:55

I have gone through many questions asked here regarding changing url without reloading. Whilst I tried almost all of the methods, I am failed to fully change the url of a page i

2条回答
  •  被撕碎了的回忆
    2021-01-29 13:31

    You can use pushState to do what you describe. As an example (pulled from here: https://www.aspsnippets.com/Articles/Change-URL-in-Browser-Address-Bar-without-reloading-using-JavaScript-and-jQuery.aspx)

    
    
    
    
    

    Note that this will not work if you are trying to open a test file directly in your browser (i.e., if the URL will be file://whatever). The browser behaves differently. You will need to serve it via some http server.

    Edit This doesn't answer the specific question asked by the OP. It will change the path, but you cannot change the hostname portion of a URL via pushState. As someone else already answered, it is not possible to change that without reloading the page due to the Same-origin policy. I left this here in case others just needed to change path portion of the URL.

提交回复
热议问题