jQuery 插件 HISTORY.js ( https://github.com/browserstate/History.js/ ) 提供了 HTML5 历史推送实现功能,如果浏览器不支持,应该能够实现 HTML4 标签功能。文档/README 文件详细说明了用法:
var History = window.History; // Note: We are using a capital H instead of a lower h
if ( !History.enabled ) {
// History.js is disabled for this browser.
// This is because we can optionally choose to support HTML4 browsers or not.
return false;
}
如您所见,该文档针对 HTML5 解释了 HISTORY.js 插件的用法,并没有解释 HTML4 支持的用法。
但是,在文档的“下载和安装”部分下,它显示为:
5. Include History.js
<script src="http://www.yourwebsite.com/history.js/scripts/compressed/history.js">/script>
<script src="http://www.yourwebsite.com/history.js/scripts/compressed/history.html4.js"></script>
此处的说明可能表示 HTML4 主题标签支持是自动的,但使用页面上的说明表明必须手动实现;我相信事实确实如此。
我找不到任何关于实现 HTML4 主题标签功能的进一步文档。请帮我解决这个问题。
最佳答案
好吧,也许问题在于您没有以正确的方式使用 History.js(这也是我遇到的问题)。基本上,要以正确的方式使用 History.js,您必须执行以下操作:
// Register navigation click handlers where you will load Ajax content
$( window ).on( 'click', 'a.ai1ec-load-view', handle_click_on_link_to_load_view );
// Bind to the statechange event
$( window ).bind( 'statechange', handle_state_change );
// When the state changes, load the corresponding view
var handle_state_change = function( e ) {
var state = History.getState();
load_view( state.url, 'json' );
};
// When clicking on a link you want to load, trigger statechange by changing state
var handle_click_on_link_to_load_view = function( e ) {
e.preventDefault();
History.pushState( { target :this }, null, $( this ).attr( 'href' ) );
};
在这样做之前,我没有听statechange
,我只是在链接点击处理程序中使用 pushState()。
如果你这样做就不需要编写后备代码,它也可以在 html4 浏览器中工作(并且来自 html4 浏览器的书签将按预期工作)
关于jquery - 实现 History.js HTML4 回退,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7186466/