如何检测pushState函数的浏览器不兼容性

时间:2012-01-14 21:38:57

标签: javascript html pushstate

此功能禁用链接的默认操作,并使用pushState函数更改URL。我需要能够检测浏览器是否不支持此功能,以便我可以停止preventDefault()函数。

$("a").click(function(event) {      

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

        // Disable Default Action and Change the URL -  
        event.preventDefault();     
        window.history.pushState("somedata", "Title", url);

        //Call Function to change the content - 
        loadContent(url);
    });

非常感谢任何建议

1 个答案:

答案 0 :(得分:7)

使用特征检测:

if (history.pushState) {
  // supported.
}

示例:

$("a").click(function(event) {      
    var url = "";
    var url = $(this).attr('href'); 

    if (history.pushState) {
        window.history.pushState("somedata", "Title", url);
        event.preventDefault();
    }

    //Call Function to change the content - 
    loadContent(url);
});
相关问题