JavaScript:导航到新的 URL,而不替换历史记录中的当前页面(不是 window.location)

发布于 2024-08-23 00:34:54 字数 869 浏览 4 评论 0原文

我知道如何使用 window.location。问题是,(至少在 FF 中)它会从历史记录中删除您所在的页面。例如,我在页面 A 上,导航到 B 并使用 window.location 将我发送到 C。如果在页面 C 上,我单击返回,我最终会到达 A,而不是 B。

还有其他导航方式吗到另一个页面而不从历史记录中删除当前页面?

编辑:如果您在 Firebug 中运行代码,则不会发生这种情况。

这是一个超级简单的页面(是的,我知道代码很丑陋,它不是真正的代码),显示了问题:

<html>
<head><script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"> </script>
<body>  
<div class="foo"></div>
<a href="javascript:$('.foo').html('<scri'+'pt type=&quot;text/javascript&quot;>window.location = &quot;c.html&quot;</s'+'cript>');">b</a>

EDIT2:jQuery 是问题,setTimeout 是答案。我在下面添加了我的解决方案,以防其他人遇到这种奇怪的边缘情况。

EDIT3:这个例子被简化以使其更容易测试。在实际情况中,单击处理程序会进行返回 HTML 的 Ajax 调用,因此我们不能仅通过编写 window.location = another 来简化整个过程。代码来自服务器,嵌入在 HTML 中。

I know how to use window.location. The problem is that, (at least in FF) it erases the page you are on from the history. e.g., I'm on page A, I navigate to B and window.location is used to send me to C. If on page C, I click back, I end up at A, not B.

Is there some other way to navigate to another page without erasing the current page from the history?

EDIT: This wont happen if you run the code in Firebug.

Here is a super simple page (yes I know the code is ugly, it's not the real code) that shows the problem:

<html>
<head><script type="text/javascript" src="http://code.jquery.com/jquery-1.4.2.min.js"> </script>
<body>  
<div class="foo"></div>
<a href="javascript:$('.foo').html('<scri'+'pt type="text/javascript">window.location = "c.html"</s'+'cript>');">b</a>

EDIT2: jQuery is the problem and setTimeout is the answer. I've added my solution below just in case someone else runs into this weird edge case.

EDIT3: This example is simplified to make it easier to test. In the real case, the click handler makes an Ajax call that returns the HTML, so we can't simplify the whole thing by just writing window.location = whatever. The code comes from the server, embedded in the HTML.

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

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

发布评论

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

评论(5

走野 2024-08-30 00:34:54

setTimeout 就是答案。

$(div).html('<script...> setTimeout(function() { window.location = url; },10); </script>');

没有这个问题。它一定与 jQuery 执行内联脚本的方式有关(通过在 head 元素中创建 script 标记)。

setTimeout is the answer.

$(div).html('<script...> setTimeout(function() { window.location = url; },10); </script>');

Doesn't have this problem. It must have something to do with the way jQuery executes inline scripts (by creating a script tag in the head element).

云仙小弟 2024-08-30 00:34:54

您可以使用window.location.assign(URL)

You could use window.location.assign(URL).

美人骨 2024-08-30 00:34:54

我在 Firefox 3.6 中没有看到这种行为。按照您的示例,我从 stackoverflow.com 访问 google.com,然后从那里使用 Firebug 控制台窗口设置 window.location = 'http://facebook.com'。到达那里后,所有三个都在我的浏览器历史记录中。

I'm not seeing that behavior in Firefox 3.6. Following your example, from stackoverflow.com I went to google.com, then from there I used the Firebug console window to set window.location = 'http://facebook.com'. Once there, all three were in my browser history..

囍笑 2024-08-30 00:34:54

这只是一个 hack,我不知道它是否会起作用。使用 window.location = URL; 设置 URL 后尝试 window.history.back(1)

希望这会有所帮助。

This is just a hack and I don't know if it will work. Try window.history.back(1) after you have set the URL using window.location = URL;

Hope this helps.

感情旳空白 2024-08-30 00:34:54

使用 location.href 而不是位置。

Use location.href instead of location.

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