3

此函数禁用链接的默认操作,并使用 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);
    });

非常感谢任何建议

4

1 回答 1

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);
});
于 2012-01-14T21:42:12.483 回答