【问题标题】:Implementing History.js HTML4 Fallback实现 History.js HTML4 后备
【发布时间】:2023-03-09 21:02:01
【问题描述】:

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;
    }

如你所见,文档解释了 HISTORY.js 插件的使用到 HTML5 的点,并没有解释 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 主题标签功能的进一步文档。请帮我解决这个问题。

【问题讨论】:

  • 按照你写这个的方式,它可能会被关闭。请解释您尝试过的方法,并提供明确的示例说明哪些方法不起作用以及您需要帮助的地方。否则,请阅读该工具的文档,并寻找论坛进行更一般的讨论。 See "What kind of questions should I not ask here?"
  • 好的,抱歉。谢谢你告诉我我做错了什么。我只是想,如果我写的太多,人们不会读它。我将编辑帖子以进一步解释。
  • 你试过了吗?听起来插件会自动优雅地降级(它可以在没有额外实现的情况下与 HTML4 一起使用)。
  • 似乎没有人回答这个问题
  • 这正是我一直在寻找的东西

标签: jquery html jquery-plugins history.js


【解决方案1】:

好吧,也许问题是您没有以正确的方式使用 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 浏览器的书签将按预期工作)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-08
    • 1970-01-01
    • 2011-05-10
    相关资源
    最近更新 更多