首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >实施History.js HTML4回退

实施History.js HTML4回退
EN

Stack Overflow用户
提问于 2011-08-25 15:17:24
回答 1查看 3.6K关注 0票数 6

jQuery plugin HISTORY.js (https://github.com/browserstate/History.js/)提供了HTML5历史推送实现功能,在不支持浏览器的情况下,应该能够实现HTML4标签功能。文档/自述文件详细说明了用法:

代码语言:javascript
复制
   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支持的用法。

但是,在文档的"Download & Installation“部分,它显示:

代码语言:javascript
复制
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标签特性的文档。请帮我弄清楚这件事。

EN

回答 1

Stack Overflow用户

发布于 2012-09-27 22:45:39

好吧,也许问题是你没有以正确的方式使用History.js (这也是我遇到的问题)。基本上,要以正确的方式使用History.js,您必须这样做:

代码语言:javascript
复制
// 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浏览器的书签也会按预期工作)

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7186466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档