重写 document.location 而不加载

发布于 2024-09-03 14:59:18 字数 133 浏览 2 评论 0 原文

我正在开发一个 100% ajax 的系统,当然第一个请求除外。

我有兴趣使用 javascript 更改 document.location 中的地址。 但我不希望浏览器在该“新”位置加载页面。

有谁知道我该怎么做?

I am developing a system which is 100% ajax, except of course, the first request.

I am interested in changing the address in document.location, using javascript.
But I don't want the browser to load the page on that "new" location.

Does anyone know how I can do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

很酷又爱笑 2024-09-10 14:59:18

要重写整个位置而不仅仅是“哈希”部分,可以使用浏览器历史记录 API,尽管目前它似乎只是 Gecko 1.9.3/Firefox 4 支持

history.replaceState({}, document.title, url)

To rewrite the entire location and not just the "hash" part, browser history API can be used, although currently it only seems to be supported in Gecko 1.9.3/Firefox 4.

history.replaceState({}, document.title, url)
翻身的咸鱼 2024-09-10 14:59:18

在不导航的情况下更改整个 URL 是不可能的,想象一下它可能产生的安全问题

您只能更改 location.hash,它是 URL 中 # 符号后面的部分:

location.hash = "foo";

您的 url 将更改为 http://someurl.com/#foo

Changing the entire URL without navigating is not possible, just imagine the security issues that it could generate.

You can change only the location.hash, which is the part of the URL that follows the # symbol:

location.hash = "foo";

Your url will change to http://someurl.com/#foo

温柔少女心 2024-09-10 14:59:18

您可以使用与 Gmail 相同的方法。在 url 末尾附加一个锚点,浏览器不应重新加载页面,但您可以读取 document.location.href 中的信息并对其进行操作。这也将保持后退按钮的功能完整(前提是您的 JavaScript 支持),

例如

首页是 http://www.mypage.com/index.php 您单击到下一个“页面” " 使用 link ,它会更改为 http://www.mypage.com/index.php#page2

You can use the same method that Gmail uses. Append an anchor to the end of the url, the browser should not reload the page but you can read the information in document.location.href and act on it. This also will keep the functionality of the back button intact (providing your javascript supports it)

for example

first page is http://www.mypage.com/index.php you click to the next "page" using <a href="#page2">link</a> and it changes to http://www.mypage.com/index.php#page2

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文