如何使用 Javascript 和 JQuery 操作 URL?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6601609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 21:14:35  来源:igfitidea点击:

How to manipulate the URL with Javascript and JQuery?

javascriptjqueryurl

提问by Hugo Mota

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.

我想制作一个包含大量 Javascript 交互的页面。然而,当用户浏览页面时,URL 也必须改变。因此,当用户共享 URL 或保存 URL 时,它可以将他带到他的实际状态。

How can I do that?

我怎样才能做到这一点?

Examples:

例子:

myapp.com/page1

myapp.com/page1

myapp.com/page2

myapp.com/page2

回答by Quentin

pushState, as seen on github

pushState,如在github 上看到的

回答by GolezTrol

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.

您可以在不重新加载页面的情况下更改 url(或位置)的唯一部分是哈希。那是#. 许多 ajax 增强应用程序都利用了这一点,包括 Twitter。您可以随时更改此哈希值,并在页面加载时解释哈希标记以将页面初始化为正确状态。

回答by Mark Pope

Set this value: window.location.href

设置这个值:window.location.href

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