判斷ie瀏覽器七、八、9三個版本(轉)

上午的時候,原本是想作一個position:fixed在各個瀏覽器下兼容的方案的,可是發現ie7/8下面的position:fixed只支持一個屏幕,若是內容高度超過一個屏幕就不能很好的使用position:fixed了(若是哪一個同窗有position:fixed的完美方案麻煩給我一下),因而我想用js來兼容,其實就是經過scroll監聽來實現,因而須要判斷ie7/8這倆版本,可是發現jquery的$.browser.version並不支持ie8,因而我就百度,百度卻是給出不少答案,可是發現ie8仍是沒能檢測出來,後來我本身查看了navigator.userAgent這個東西,在各個瀏覽器下面打出來是這個樣子的:javascript

//ie9 : Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
//ie8 : Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)
//ie7 : Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; Trident/5.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; InfoPath.3; Tablet PC 2.0; .NET4.0E; .NET4.0C)
//Mozi: Mozilla/5.0 (Windows NT 6.1; rv:20.0) Gecko/20100101 Firefox/20.0
//goog: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22
//oper: Opera/9.80  (Windows NT 6.1; Edition IBIS) Presto/2.12.388 Version/12.14
//appl: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/534.57.2 (KHTML, like Gecko) Version/5.1.7 Safari/534.57.2

  你就發現,ie8和ie9是同樣的,這下子蒙了,看來光靠這個navigator.userAgent是不夠了,可是我相信你也很快就知道怎麼作了,由於i9仍是一個比較先進的瀏覽器,擁有一些ie8沒有的屬性,我不一一羅列了,其中一個就是window.innerWidth,這個屬性值是瀏覽器的內高度(不包括工具欄和滾動條,仍是一個比較有用的屬性)。ie8下面這個屬性值是undefined,9下面就ok了,顯示的是一個數字。因而判斷瀏覽器版本就很天然了,看下面這個方案:java

 

if(navigator.userAgent.indexOf("MSIE")>0){   
      if(navigator.userAgent.indexOf("MSIE 6.0")>0){   
        alert("ie6");    
      }   
      if(navigator.userAgent.indexOf("MSIE 7.0")>0){  
        alert("ie7");   
      }   
      if(navigator.userAgent.indexOf("MSIE 9.0")>0 && !window.innerWidth){//這裏是重點,你懂的
        alert("ie8");  
      }   
      if(navigator.userAgent.indexOf("MSIE 9.0")>0){  
        alert("ie9");  
      }   
    } 
相關文章
相關標籤/搜索