使用 jQuery 更改浏览器地址栏 URL

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9497677/
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-08-27 10:54:04  来源:igfitidea点击:

Change browser address bar URL with jQuery

ajaxjquery

提问by oshirowanen

Is it possible to change the URL in the browser address bar directly with jQuery without refreshing the page or redirecting to the changed url?

是否可以在不刷新页面或重定向到更改后的 url 的情况下,直接使用 jQuery 更改浏览器地址栏中的 URL?

Or is this not possible due to security issues?

或者由于安全问题这是不可能的?

For example, I have a list which is jquery.ajax driven so no post backs/screen refreshes. The list has a paging element. So for the first page the url in the browser address bar is:

例如,我有一个由 jquery.ajax 驱动的列表,因此没有回传/屏幕刷新。该列表有一个分页元素。所以对于第一页,浏览器地址栏中的 url 是:

http://company/list.php?page=1

If I click on page 2 from the paging section of the list, it displays the list based on page 2, but as this is happening ajax style without refreshing/posting back, the browser url remains at

如果我从列表的分页部分单击第 2 页,它会显示基于第 2 页的列表,但是由于这是 ajax 样式而不刷新/回发,浏览器 url 保持在

http://company/list.php?page=1

I want to be able to change it to

我希望能够将其更改为

http://company/list.php?page=2

without posting back/redirecting to the new url

无需回发/重定向到新网址

Is this not possible?

这不可能吗?

采纳答案by Tim

This can only be done in more modern browsers (Chrome, Safari, FF4+, and IE10pp3+)

这只能在更现代的浏览器(Chrome、Safari、FF4+ 和 IE10pp3+)中完成

See this question: Updating address bar with new URL without hash or reloading the pagefor information on how to do it.

请参阅此问题:使用新 URL 更新地址栏而不使用哈希或重新加载页面以获取有关如何执行此操作的信息。

回答by Tx3

Maybe with some modification you could use Fragment Identifieraka Hash (# sign in the Url) to change (part of) Url without page change.

也许通过一些修改,您可以使用Fragment Identifieraka Hash(在 Url 中签名)来更改(部分)Url,而无需更改页面。

Normally this is used to automatically navigate user to certain chapter in the page or initialize control to certain state. For example automatically select correct tab in the tab control. Ben Alman has an exampleabout that (using jQuery BBQ).

通常这用于自动将用户导航到页面中的特定章节或将控件初始化为特定状态。例如在选项卡控件中自动选择正确的选项卡。Ben Alman 有一个例子(使用 jQuery BBQ)。