如何使用 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
How to manipulate the URL with Javascript and JQuery?
提问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 Mark Pope
Answered by this SO question: Change the URL in the browser without loading the new page using JavaScript
由这个 SO 问题回答:使用 JavaScript 更改浏览器中的 URL 而不加载新页面
回答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";