2012-01-14 71 views
3

此函數禁用鏈接的默認操作,並使用pushState函數更改URL。我需要能夠檢測瀏覽器是否不支持此功能,以便我可以停止preventDefault()函數。如何檢測pushState功能的瀏覽器不兼容

$("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); 
    }); 

任何建議是不勝感激

回答

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); 
}); 
+1

哇,簡單?非常感謝 – TaylorMac 2012-01-14 21:47:40

+0

它會讓我接受 – TaylorMac 2012-01-14 21:47:47