开发者

How to manipulate the URL with Javascript and JQuery?

开发者 https://www.devze.com 2023-03-18 06:52 出处:网络
I want to make a page with a lot of Javascript interactions. However, while a user navigates through the page the URL must change too. So, when the user shares the URL or saves it, it can lead him to

I want to make a page with a lot of Javascript interactions. However, while a user navigates through the page the URL must change too. So, when the user shares the URL or saves it, it can lead him to the actual state he was.

How can I do that?

Exa开发者_如何学编程mples:

myapp.com/page1

myapp.com/page2


pushState, as seen on github


Answered by this SO question: Change the URL in the browser without loading the new page using JavaScript


The only part of the url (or location) that you can change without reloading the page, is the hash. That is the part behind the #. Many ajax enhanced applications make use of this, including Twitter. You can change this hash on the go, and interpret the hash tag on page load to initialize the page to the correct state.


Set this value: window.location.href

  window.location.href = "myapp.com/page2";
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号