準確獲取低版本IE瀏覽器的版本號

admin發表於2017-03-31

本章節介紹一下如何如何準確獲取低版本IE瀏覽器的版本號碼。

在IE11以前判斷IE瀏覽器或者獲取版本號碼都是通過msie這麼一個東西實現的,但是在IE11中已經消失。

下面就分享一段程式碼能夠準確的獲取IE6-IE8版本號碼。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!DOCTYPE html>
<html>
<head>
<meta charset=" utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<script type="text/javascript"> 
var browser=(function(){
  var isIE6=/msie 6/i.test(navigator.userAgent);
  var isIE7=/msie 7/i.test(navigator.userAgent);
  var isIE8=/msie 8/i.test(navigator.userAgent);
  var isIE=/msie/i.test(navigator.userAgent);
  return {
    msie:isIE,
    version:function(){
      switch(true){
        case isIE6:return 6;
        case isIE7:return 7;
        case isIE8:return 8;
      }
    }()
  };
})();
window.onload=function(){
  var odiv=document.getElementById("show");
  odiv.innerHTML=browser.version;
}
</script>
</head>
<body>
<div id="show"></div>
</body>
</html>

上面的程式碼實現了獲取版本號的功能,程式碼比較簡單。

相關文章