JavaScript:浏览到新的URL,而不替换历史记录中的当前页面(不是window.location) [英] JavaScript: Navigate to a new URL without replacing the current page in the history (not window.location)

查看:136
本文介绍了JavaScript:浏览到新的URL,而不替换历史记录中的当前页面(不是window.location)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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?

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

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=&quot;text/javascript&quot;>window.location = &quot;c.html&quot;</s'+'cript>');">b</a>

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

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.

简化了此示例,使其更易于测试.在实际情况下,单击处理程序会进行Ajax调用以返回HTML,因此我们不能仅通过编写window.location = any来简化整个过程.该代码来自服务器,嵌入在HTML中.

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.

推荐答案

setTimeout是答案.

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

没有这个问题.它必须与jQuery执行内联脚本的方式有关(通过在head元素中创建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).

这篇关于JavaScript:浏览到新的URL,而不替换历史记录中的当前页面(不是window.location)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆