我可以阻止history.popstate在初始页面加载时触发吗?
我正在开发一个通过 AJAX 提供内容的网站。
如果您单击菜单中的某个项目,内容 div 会使用 $.get
响应进行更新,没什么花哨的。
我正在实现 history.pushState
以允许使用浏览器的后退/前进按钮进行导航。
我有以下内容来加载历史导航上的内容:
$(function() {
$(window).bind("popstate", function() {
$.getScript(location.href);
});
});
问题是,当第一次加载页面时,此函数执行 $.getScript
,因此页面会立即再次加载。第一次加载页面时,它会呈现初始 HTML 视图,然后在第二次加载时,它会呈现 JS 视图,因为它是 JS 请求。
如何防止此事件在包含 HTML 请求的页面上触发?
I'm working on a site that serves content via AJAX.
If you click an item in the menu, a content div gets updated with $.get
response, nothing fancy.
I'm implementing history.pushState
to allow navigation with the browser's back/forward button.
I have the following to load content on history navigation:
$(function() {
$(window).bind("popstate", function() {
$.getScript(location.href);
});
});
The problem is, when a page is loaded for the first time, this function does $.getScript
so the page is loaded immediately again. The first time the page is loaded it renders the initial HTML view, then on the second load it renders the JS view, since its a JS request.
How could I prevent this event from firing on pages with HTML requests?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
使用本机 HTML5 History API,您将遇到一些问题,每个 HTML5 浏览器处理 API 的方式都略有不同,因此您不能只编写一次代码并期望它在不实现解决方法的情况下工作。 History.js 为 HTML5 History API 提供跨浏览器 API 和可选的
hashchange
后备对于 HTML4 浏览器,如果您想走这条路。要将您的网站升级为 RIA/Ajax 应用程序,您可以使用以下代码片段:
https://github.com/browserstate/ajaxify
这是较长文章的一部分 智能状态处理,其中介绍了有关 hashbang、哈希和 html5 历史 api 的说明。
如果您需要任何进一步的帮助,请告诉我:)干杯。
Using the native HTML5 History API you're going to run into some problems, every HTML5 browser handles the API a little bit differently so you can't just code it once and expect it to work without implementing workarounds. History.js provides a cross-browser API for the HTML5 History API and a optional
hashchange
fallback for HTML4 browsers if you want to go down that route.For upgrading your website into a RIA/Ajax-Application you can use this code snippet:
https://github.com/browserstate/ajaxify
Which is part of the longer article Intelligent State Handling which goes into explanations about hashbang, hashes and the html5 history api.
Let me know if you need any further help :) Cheers.
您需要获取 event.originalEvent
You need to get the event.originalEvent
当 popstate 事件在页面加载时触发时,它的事件对象中不会有 state 属性。这允许您检查是否为页面加载触发事件。
When the popstate event is fired on page load it will not have a state property in the event object. This allows you to check if the event is fired for a page load or not.
当浏览器首次加载时,它总是会触发 popstate 事件。所以你需要确定这个popstate是否是你的。
当你执行pushState时,确保你有一个状态对象。这样您以后就可以检查一下。
然后在 popstate 处理程序上,检查状态对象 :)
如果您需要更多帮助,请告诉我 :)
When the browser first loads, it always fires a popstate event. So you need to determine if this popstate is yours or not.
When you do your pushState, make sure you have a state object. That way you can check it later.
Then on the popstate handler, check the state object :)
Let me know if you need any more help :)
我使用下面的方法来更改地址栏并保存当前状态(包括当前 HTML 正文),然后在单击后退按钮时重新加载它,而无需任何其他 AJAX 调用。所有内容都会保存在您的浏览器中:
I use the below approach to change the address bar and save the current state including the current HTML body, and I reload it on the back button click without any other AJAX call. All gets saved in your browser: