Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/69.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 同时使用本机HistoryAPI和History.js_Javascript_Jquery_Html_History.js - Fatal编程技术网

Javascript 同时使用本机HistoryAPI和History.js

Javascript 同时使用本机HistoryAPI和History.js,javascript,jquery,html,history.js,Javascript,Jquery,Html,History.js,我是一个有点困惑的历史API新手。请帮助我理解这一点。我使用本机HistoryAPI编写了以下代码: function addClicker(link) { link.removeEventListener('click', null); link.addEventListener("click", function (e) { if (swapData(link.href)) { if (prevlink != link.href) {

我是一个有点困惑的历史API新手。请帮助我理解这一点。我使用本机HistoryAPI编写了以下代码:

function addClicker(link) {
    link.removeEventListener('click', null);
    link.addEventListener("click", function (e) {
        if (swapData(link.href)) {
            if (prevlink != link.href) {
                history.pushState(null, null, link.href);
                prevlink = link.href;
            }
            e.preventDefault();
        }
    }, true);
}

function setupHistoryClicks() {
    var links = document.getElementsByClassName('hrlinks');
    for (var i = 0; i < links.length; i++)
        addClicker(links[i]);
}

function initHist() {
    if (!supports_history_api()) { return; }
    setupHistoryClicks();
    window.setTimeout(function () {
        window.addEventListener("popstate", function (e) {
            swapData(location.href); //a function that changes content according to URL
        }, false);
    }, 1);
    swapData(location.href);
}
这段代码覆盖了以hrlinks作为类的链接的click事件,并使它们能够使用historyapi

但在过去的两天里,我一直在研究HistoryAPI,我知道它不太可靠。另一种选择是使用History.js


我想知道的是,当History API不可用时,如何使用History.js?我需要为此更改代码吗?或者,当本机历史API不受支持时,我必须简单地导入History.js?

您的swapdata函数在做什么?@sun我已经在代码的注释中编写了它,它只是一个根据url更改页面内容的函数。无论如何,History.js的工作方式是在mozilla和chrome的url中添加一个哈希值。并且为ie添加了一个隐藏的iframe。因此,当您使用浏览器返回时,它将从hash或iframe中获取与浏览器相关的更改,并使用相应的参数调用您的函数。我知道,但如何将其与本机历史api一起使用?我的意思是,我想将它用于那些不支持本机历史api的浏览器,我还想知道我需要更改多少代码?我认为使用历史而不是当前代码的历史不需要太多更改