判斷ie瀏覽器7、8、9三個版本

來源:互聯網
上載者:User

標籤:

上午的時候,本來是想做一個position:fixed在各個瀏覽器下相容的方案的,但是發現ie7/8下面的position:fixed只支援一個螢幕,如果內容高度超過一個螢幕就不能很好的使用position:fixed了(如果哪個同學有position:fixed的完美方案麻煩給我一下),於是我想用js來相容,其實就是通過scroll監聽來實現,於是需要判斷ie7/8這倆版本,但是發現jquery的$.browser.version並不支援ie8,於是我就百度,百度倒是給出很多答案,但是發現ie8還是沒能檢測出來,後來我自己查看了navigator.userAgent這個東西,在各個瀏覽器下面打出來是這個樣子的:

//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了,顯示的是一個數字。於是判斷瀏覽器版本就很自然了,看下面這個方案:

 

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

  

判斷ie瀏覽器7、8、9三個版本(轉)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.