JavaScript:导航到新的 URL,而不替换历史记录中的当前页面(不是 window.location)
我知道如何使用 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="text/javascript">window.location = "c.html"</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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
setTimeout
就是答案。没有这个问题。它一定与 jQuery 执行内联脚本的方式有关(通过在
head
元素中创建script
标记)。setTimeout
is the answer.Doesn't have this problem. It must have something to do with the way jQuery executes inline scripts (by creating a
script
tag in thehead
element).您可以使用
window.location.assign(URL)
。You could use
window.location.assign(URL)
.我在 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..
这只是一个 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 usingwindow.location = URL;
Hope this helps.
使用
location.href
而不是位置。Use
location.href
instead of location.