DIV CSS 佈局教程網

 DIV+CSS佈局教程網 >> 網頁腳本 >> JavaScript入門知識 >> 關於JavaScript >> JS判斷浏覽器類型與版本的實現代碼
JS判斷浏覽器類型與版本的實現代碼
編輯:關於JavaScript     
在眾多的浏覽器產品中,IE、Firefox、Opera、Safari........眾多品牌卻標准不一,因此時常需要根據不同的浏覽器,甚至相同浏覽器不同版本做不同的操作,因此,知曉浏覽器的判斷方法,還是很重要的。下面列舉一下常用的判斷方法

1、判斷浏覽器是否為IE

document.all ? 'IE' : 'others':在IE下document.all值為1,而其他浏覽器下的值為0;
navigator.userAgent.indexOf("MSIE")>0 ? 'IE' : 'others':navigator.userAgent是描述用戶代理信息。
navigator.appName.indexOf("Microsoft") != -1 ? 'IE' : 'others':navigator.appName描述浏覽器名稱信息。

2、判斷IE版本

navigator.appVersion.match(/6./i)=="6." ? 'IE6' : 'other version':在已知是IE浏覽器的情況下,可以通過此方法判斷是否是IE6;
navigator.userAgent.indexOf("MSIE 6.0")>0 ? 'IE7' : 'other version':同上;
navigator.appVersion.match(/7./i)=="7." ? 'IE7' : 'other version':在已知是IE浏覽器的情況下,可以通過此方法判斷是否是IE7;
navigator.userAgent.indexOf("MSIE 7.0")>0 ? 'IE7' : 'other version':同上;
navigator.appVersion.match(/8./i)=="8." ? 'IE8' : 'other version':在已知是IE浏覽器的情況下,可以通過此方法判斷是否是IE8;
navigator.userAgent.indexOf("MSIE 8.0")>0 ? 'IE8' : 'other version':同上。

3、JS獲取浏覽器信息

浏覽器代碼名稱:navigator.appCodeName
浏覽器名稱:navigator.appName
浏覽器版本號:navigator.appVersion
對Java的支持:navigator.javaEnabled()
MIME類型(數組):navigator.mimeTypes
系統平台:navigator.platform
插件(數組):navigator.plugins
用戶代理:navigator.userAgent

DEMO:
Js代碼

[Ctrl+A 全選 注:如需引入外部Js需刷新才能執行]
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved