- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在函数内使用window.history.pushState({)
。这按预期工作得很好。下面是一个示例。
$('#go_btn').click(function(){
if ($('.green')) {
var newurl = "cutomurl.html";
if(newurl!=window.location){
window.history.pushState({path:newurl},'',newurl);
}
return false;
}
});
并且我还获得了以下代码,以便在 $('#go_btn')
按钮触发 newurl
时刷新页面。
$(window).on('popstate', function(){
location.reload(true);
});
在桌面版本上我似乎没有任何问题。但是,当我尝试在 iPad 上加载此页面时,它会不断地重新加载该页面。那么如何停止持续重新加载并仅在触发 window.history.pushState({path:newurl},'',newurl);
时才刷新页面?
最佳答案
Note that just calling history.pushState() or history.replaceState() won't trigger a popstate event. The popstate event is only triggered by doing a browser action such as clicking on the back button (or calling history.back() in JavaScript). And the event is only triggered when the user navigates between two history entries for the same document.
Browsers tend to handle the popstate event differently on page load. Chrome (prior to v34) and Safari always emit a popstate event on page load, but Firefox doesn't.
According to Mozzila Developer Network
https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onpopstate
Safari 总是在页面加载时发出 popstate 事件。这意味着
更新:请阅读有关操纵浏览器历史记录的文档。 https://developer.mozilla.org/en-US/docs/Web/Guide/API/DOM/Manipulating_the_browser_history
看来您误解了某些功能。
您有一个#go_btn 按钮。当您单击它时,您希望页面能够:
使用location.reload()
if (newurl != window.location)
{
// window.history.pushState({path:newurl},'',newurl);
location.reload(true);
}
使用location.href
。它将在当前窗口中打开新页面。
if (newurl != window.location)
{
// window.history.pushState({path:newurl},'',newurl);
location.href = newurl;
}
使用window.history.pushState()
或window.history.replaceState()
。它不会打开新页面或重新加载它。仅当您不打算导航但希望用户获取新 URL(方便浏览、历史记录或收藏夹)时才使用它。
if (newurl!=window.location)
{
window.history.pushState({path:newurl},'',newurl);
}
在所有这三种情况下,您都不需要 popstate
事件。它用于其他目的。
您需要哪一个?这取决于你的任务。
无论如何,将它们中的任何两个一起使用并不是不合逻辑且无用的。如果要刷新页面,推送历史状态的原因是什么?然后导航到它。
关于javascript - 防止页面不断重新加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30610181/
如果这不是一个错误,那就是另一个错误。如果不是那样的话,那就是别的东西了。我觉得我的项目已经改变了很多,现在只是试图解决代码签名问题,结果一切都搞砸了。我严格按照说明进行操作,但出现错误,例如当前的“
我不确定是否有一些我不知道的内置变量或规则,或者 make 是否有问题,或者我只是疯了。 对于我的一个项目,我有一个如下的 makefile: CC=g++ CFLAGS=-O3 `libpng-co
我有大约 10 个 div,它们必须不断翻转,每个 div 延迟 3 秒 这个 codrops 链接的最后一个效果是我正在寻找的,但无需单击 div http://tympanus.net/Devel
我如何使用 jQuery 持续运行 PHP 脚本并每秒获取响应,以及将鼠标上的少量数据发送到同一脚本? 我真的必须添加一些随机扩展才能让这么简单的计时器工作吗? 最佳答案 To iterate is
JBoss 4.x EJB 3.0 我见过如下代码(大大简化): @Stateless @TransactionAttribute(TransactionAttributeType.NOT_SUPPO
使用 PHPStorm,我试图忽略每次尝试进行 git 提交时 pop 的 workspace.xml。 我的 .gitignore 看起来像: /.idea/ .idea/workspace.xml
我是一名优秀的程序员,十分优秀!