History.js问题

时间:2011-11-07 19:24:01

标签: javascript html5 history.js

我的历史API工作,但我正在尝试实现History.js,但我似乎无法访问state.data。我想在state.data中使用字符串来调用相应的函数。虽然我的概念适用于History API,但它不适用于History.js,我的代码位于以下位置:

(function (window, undefined) {
    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;
    }

    // Bind to StateChange Event
    History.Adapter.bind(window, 'statechange', function () { // Note: We are using statechange instead of popstate
        var State = History.getState(); // Note: We are using History.getState() instead of event.state
        alert(State.data);
        if (State.data != null) {
            var strFun = State.data;
            alert(strFun);
            //Create the function call from function name and parameter.
            var mySplitResult = strFun.split(",");
            //var strParam = "null";
            //Call the function and arguments 
            window[mySplitResult[0]](mySplitResult[1], mySplitResult[2]);
        }
    });
})(window);

我的链接是这样的:

<li>
  <a href="#" onclick="javascript:History.pushState({state:newarticles},'New Articles','newarticle'); return false;">Articles</a>
</li>
<li>
  <a href="#" onclick="javascript:History.pushState({state:displaybookmarks},'Favourties','favourties'); return false;">Favourites</a>
</li>
<li>
  <a href="#" onclick="javascript:History.pushState({state:pagenation},Listing,1,'Archive','archieve1'); return false;">Archive</a>
</li>

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

调用每个的正确方法如下......请注意,onclick没有javascript:,并且引用了状态数据!

<a href="#" onclick="History.pushState({state:'newarticles'},'New Articles','newarticle'); return false;"&gt;Articles&lt;/a>

希望有所帮助! PETE