使用history.js来检测是否支持HTML4,这样我就可以绕过ajax了

时间:2014-08-29 17:51:23

标签: javascript browser history.js html4

history.js在我的网站上,在支持HTML5的浏览器中完美运行。单击URL时,它会执行ajax调用,并在完成时将状态保存到历史记录中。我使用函数update_content()淡入新内容。

在HTML4浏览器上,我只是希望网站将URL视为普通URL并跳过AJAX。我希望history.js有一个我可以调用来检测浏览器的方法或函数。下面的伪代码。

$(document).on('click', 'a[data-type="content"]', function(event) {

    // pseudo code
    if(History.html4() ) { return; }

    event.preventDefault();

    var href = $(this).attr('href');

    $.get(href, function(data) { 

        var new_data = $(data).siblings('#main_wrapper').html();

        // update page with the new data!
        update_content(new_data);

        // add an item to the history log
        History.pushState({ html: new_data }, event.target.textContent, event.target.href);

    });

});

我只包含了html5 history.js,所以它不会尝试将#放在URL中。

我可以使用history.js中的方法或函数来检测当前浏览器是否是HTML4浏览器,所以我知道将链接视为正常吗?有更好的方法吗?

我不想安装像modernizr这样的插件,除非我必须这样做。

2 个答案:

答案 0 :(得分:2)

不测试浏览器是否支持HTML 4:这是一个非常广泛的类别。

不要测试浏览器是否支持表单提交:它们都是。

测试浏览器是否支持历史记录API。

if (!history.pushState) {
    return;
}

答案 1 :(得分:1)

如果window.historypushState方法

,您可以查看
if(!('pushState' in window.history)) {
    // doNothing();
    return;
}