JavaScript 检测浏览器和操作系统的脚本


Posted in Javascript onDecember 26, 2008

Web编程中,识别用户的浏览器以及操作系统非常重要。因为它们对HTML和JavaScript的支持程度有所不同。
一般应用在系统登陆页面或者首页中。
具体应用如下:

<html> 
<head> 
<title>Login</title> 
<script type="text/javascript" src="detect.js"></script> 
<script type="text/javascript"> 
window.onload = function () { 
if ((isMinIE5_5 && isWin) || (isMinMoz1 && isUnix) 
|| (isMinSafari1 && isMac)) { 
document.getElementById("divLogin").style.visibility = "visible"; 
document.getElementById("divError").style.visibility = "hidden" 
} 
}; 
</script> 
</head> 
<body> 
<form method="post" action="DoLogin.jsp"> 
<div style="border: 2px dashed blue; background-color: #dedede; height: 300px; padding: 10px"> 
<div id="divError" style="position: absolute; left: 20px; top: 100px; "> 
This Web application requires one of the following: 
<ul> 
<li>Internet Explorer 5.5 or higher for Windows</li> 
<li>Mozilla 1.0 or higher for Unix</li> 
<li>Safari 1.0 or higher for Macintosh</li> 
</ul> 
<noscript> 
<p>This Web application also requires JavaScript (if you are using one of the above browsers, make sure that JavaScript is enabled).</p> 
</noscript> 
</div> 
<div id="divLogin" style="position: absolute; left: 20px; top: 100px; visibility: hidden"> 
<table border="0" width="100%" height="100%"><tr><td align="center"> 
<table border="0"> 
<tr> 
<td>Username:</td><td><input type="text" name="txtUsername" /></td> 
</tr> 
<tr> 
<td>Password:</td><td><input type="password" name="txtPassword" /></td> 
</tr> 
<tr> 
<td> </td><td><input type="Submit" value="Login" /></td> 
</tr> 
</table> 
</td></tr></table> 
</div> 
</div> 
</form> 
</body> 
</html>

detect.js
/************************************************************************* 
File Name: detect.js 
Create Date: 
Author: 
Description: detect browser and operating system 
***********************************************************************/ 
var sUserAgent = navigator.userAgent; 
var fAppVersion = parseFloat(navigator.appVersion); 
function compareVersions(sVersion1, sVersion2) { 
var aVersion1 = sVersion1.split("."); 
var aVersion2 = sVersion2.split("."); 
if (aVersion1.length > aVersion2.length) { 
for (var i=0; i < aVersion1.length - aVersion2.length; i++) { 
aVersion2.push("0"); 
} 
} else if (aVersion1.length < aVersion2.length) { 
for (var i=0; i < aVersion2.length - aVersion1.length; i++) { 
aVersion1.push("0"); 
} 
} 
for (var i=0; i < aVersion1.length; i++) { 
if (aVersion1[i] < aVersion2[i]) { 
return -1; 
} else if (aVersion1[i] > aVersion2[i]) { 
return 1; 
} 
} 
return 0; 
} 
var isOpera = sUserAgent.indexOf("Opera") > -1; 
var isMinOpera4 = isMinOpera5 = isMinOpera6 = isMinOpera7 = isMinOpera7_5 = false; 
if (isOpera) { 
var fOperaVersion; 
if(navigator.appName == "Opera") { 
fOperaVersion = fAppVersion; 
} else { 
var reOperaVersion = new RegExp("Opera (\\d+\\.\\d+)"); 
reOperaVersion.test(sUserAgent); 
fOperaVersion = parseFloat(RegExp["$1"]); 
} 
isMinOpera4 = fOperaVersion >= 4; 
isMinOpera5 = fOperaVersion >= 5; 
isMinOpera6 = fOperaVersion >= 6; 
isMinOpera7 = fOperaVersion >= 7; 
isMinOpera7_5 = fOperaVersion >= 7.5; 
} 
var isKHTML = sUserAgent.indexOf("KHTML") > -1 
|| sUserAgent.indexOf("Konqueror") > -1 
|| sUserAgent.indexOf("AppleWebKit") > -1; 
var isMinSafari1 = isMinSafari1_2 = false; 
var isMinKonq2_2 = isMinKonq3 = isMinKonq3_1 = isMinKonq3_2 = false; 
if (isKHTML) { 
isSafari = sUserAgent.indexOf("AppleWebKit") > -1; 
isKonq = sUserAgent.indexOf("Konqueror") > -1; 
if (isSafari) { 
var reAppleWebKit = new RegExp("AppleWebKit\\/(\\d+(?:\\.\\d*)?)"); 
reAppleWebKit.test(sUserAgent); 
var fAppleWebKitVersion = parseFloat(RegExp["$1"]); 
isMinSafari1 = fAppleWebKitVersion >= 85; 
isMinSafari1_2 = fAppleWebKitVersion >= 124; 
} else if (isKonq) { 
var reKonq = new RegExp("Konqueror\\/(\\d+(?:\\.\\d+(?:\\.\\d)?)?)"); 
reKonq.test(sUserAgent); 
isMinKonq2_2 = compareVersions(RegExp["$1"], "2.2") >= 0; 
isMinKonq3 = compareVersions(RegExp["$1"], "3.0") >= 0; 
isMinKonq3_1 = compareVersions(RegExp["$1"], "3.1") >= 0; 
isMinKonq3_2 = compareVersions(RegExp["$1"], "3.2") >= 0; 
} 
} 
var isIE = sUserAgent.indexOf("compatible") > -1 
&& sUserAgent.indexOf("MSIE") > -1 
&& !isOpera; 
var isMinIE4 = isMinIE5 = isMinIE5_5 = isMinIE6 = false; 
if (isIE) { 
var reIE = new RegExp("MSIE (\\d+\\.\\d+);"); 
reIE.test(sUserAgent); 
var fIEVersion = parseFloat(RegExp["$1"]); 
isMinIE4 = fIEVersion >= 4; 
isMinIE5 = fIEVersion >= 5; 
isMinIE5_5 = fIEVersion >= 5.5; 
isMinIE6 = fIEVersion >= 6.0; 
} 
var isMoz = sUserAgent.indexOf("Gecko") > -1 
&& !isKHTML; 
var isMinMoz1 = sMinMoz1_4 = isMinMoz1_5 = false; 
if (isMoz) { 
var reMoz = new RegExp("rv:(\\d+\\.\\d+(?:\\.\\d+)?)"); 
reMoz.test(sUserAgent); 
isMinMoz1 = compareVersions(RegExp["$1"], "1.0") >= 0; 
isMinMoz1_4 = compareVersions(RegExp["$1"], "1.4") >= 0; 
isMinMoz1_5 = compareVersions(RegExp["$1"], "1.5") >= 0; 
} 
var isNS4 = !isIE && !isOpera && !isMoz && !isKHTML 
&& (sUserAgent.indexOf("Mozilla") == 0) 
&& (navigator.appName == "Netscape") 
&& (fAppVersion >= 4.0 && fAppVersion < 5.0); 
var isMinNS4 = isMinNS4_5 = isMinNS4_7 = isMinNS4_8 = false; 
if (isNS4) { 
isMinNS4 = true; 
isMinNS4_5 = fAppVersion >= 4.5; 
isMinNS4_7 = fAppVersion >= 4.7; 
isMinNS4_8 = fAppVersion >= 4.8; 
} 
var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows"); 
var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") 
|| (navigator.platform == "Macintosh"); 
var isUnix = (navigator.platform == "X11") && !isWin && !isMac; 
var isWin95 = isWin98 = isWinNT4 = isWin2K = isWinME = isWinXP = false; 
var isMac68K = isMacPPC = false; 
var isSunOS = isMinSunOS4 = isMinSunOS5 = isMinSunOS5_5 = false; 
if (isWin) { 
isWin95 = sUserAgent.indexOf("Win95") > -1 
|| sUserAgent.indexOf("Windows 95") > -1; 
isWin98 = sUserAgent.indexOf("Win98") > -1 
|| sUserAgent.indexOf("Windows 98") > -1; 
isWinME = sUserAgent.indexOf("Win 9x 4.90") > -1 
|| sUserAgent.indexOf("Windows ME") > -1; 
isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1 
|| sUserAgent.indexOf("Windows 2000") > -1; 
isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1 
|| sUserAgent.indexOf("Windows XP") > -1; 
isWinNT4 = sUserAgent.indexOf("WinNT") > -1 
|| sUserAgent.indexOf("Windows NT") > -1 
|| sUserAgent.indexOf("WinNT4.0") > -1 
|| sUserAgent.indexOf("Windows NT 4.0") > -1 
&& (!isWinME && !isWin2K && !isWinXP); 
} 
if (isMac) { 
isMac68K = sUserAgent.indexOf("Mac_68000") > -1 
|| sUserAgent.indexOf("68K") > -1; 
isMacPPC = sUserAgent.indexOf("Mac_PowerPC") > -1 
|| sUserAgent.indexOf("PPC") > -1; 
} 
if (isUnix) { 
isSunOS = sUserAgent.indexOf("SunOS") > -1; 
if (isSunOS) { 
var reSunOS = new RegExp("SunOS (\\d+\\.\\d+(?:\\.\\d+)?)"); 
reSunOS.test(sUserAgent); 
isMinSunOS4 = compareVersions(RegExp["$1"], "4.0") >= 0; 
isMinSunOS5 = compareVersions(RegExp["$1"], "5.0") >= 0; 
isMinSunOS5_5 = compareVersions(RegExp["$1"], "5.5") >= 0; 
} 
}
Javascript 相关文章推荐
Javascript &amp; DHTML 实例编程(教程)(三)初级实例篇1—上传文件控件实例
Jun 02 Javascript
jquery 笔记 事件
Nov 02 Javascript
javascript Array.prototype.slice的使用示例
Nov 14 Javascript
js操作table示例(个人心得)
Nov 29 Javascript
js实现上传图片预览的方法
Feb 09 Javascript
JavaScript实现简单获取当前网页网址的方法
Nov 09 Javascript
Highcharts使用简例及异步动态读取数据
Dec 30 Javascript
Backbone中View之间传值的学习心得
Aug 09 Javascript
AngularJs上传前预览图片的实例代码
Jan 20 Javascript
javascript数据结构中栈的应用之符号平衡问题
Apr 11 Javascript
使用JavaScript实现在页面中显示距离2017年中秋节的天数
Sep 26 Javascript
解决vue的变量在settimeout内部效果失效的问题
Aug 30 Javascript
javascript 对表格的行和列都能加亮显示
Dec 26 #Javascript
JavaScript 仿关机效果的图片层
Dec 26 #Javascript
jquery 插件 任意位置浮动固定层
Dec 25 #Javascript
jquery 插件 web2.0分格的分页脚本,可用于ajax无刷新分页
Dec 25 #Javascript
jquery 图片预加载 自动等比例缩放插件
Dec 25 #Javascript
jquery 插件之仿“卓越亚马逊”首页弹出菜单效果
Dec 25 #Javascript
ASP SQL防注入的方法
Dec 25 #Javascript
You might like
php 多关键字 高亮显示实现代码
2012/04/23 PHP
ThinkPHP中limit()使用方法详解
2016/04/19 PHP
Yii2.0中的COOKIE和SESSION用法
2016/08/12 PHP
PHPCMS V9 添加二级导航的思路详解
2016/10/20 PHP
PHP/HTML混写的四种方式总结
2017/02/27 PHP
Thinkphp5框架实现获取数据库数据到视图的方法
2019/08/14 PHP
Javascript 获取链接(url)参数的方法[正则与截取字符串]
2010/02/09 Javascript
JQury slideToggle闪烁问题及解决办法
2011/07/05 Javascript
jQuery Ajax请求状态管理器打包
2012/05/03 Javascript
ExtJS4中使用mixins实现多继承示例
2013/12/03 Javascript
Jquery焦点图实例代码
2014/11/25 Javascript
基于bootstrap3和jquery的分页插件
2015/07/31 Javascript
javascript实现平滑无缝滚动
2020/08/09 Javascript
jQuery中delegate()方法的用法详解
2016/10/13 Javascript
详谈jQuery unbind 删除绑定事件 / 移除标签方法
2017/03/02 Javascript
JavaScript实现简单图片轮播效果
2017/08/21 Javascript
Angular学习笔记之集成三方UI框架、控件的示例
2018/03/23 Javascript
JS与jQuery判断文本框还剩多少字符可以输入的方法
2018/09/01 jQuery
vue - vue.config.js中devServer配置方式
2019/10/30 Javascript
基于javascript实现贪吃蛇小游戏
2019/11/25 Javascript
jQuery实现移动端图片上传预览组件的方法分析
2020/05/01 jQuery
浅谈使用nodejs搭建web服务器的过程
2020/07/20 NodeJs
Python 字符串操作方法大全
2014/03/11 Python
简述:我为什么选择Python而不是Matlab和R语言
2017/11/14 Python
python 动态加载的实现方法
2017/12/22 Python
PyQt5打开文件对话框QFileDialog实例代码
2018/02/07 Python
解决python3 HTMLTestRunner测试报告中文乱码的问题
2018/12/17 Python
Python Opencv实现图像轮廓识别功能
2020/03/23 Python
如何利用python给图片添加半透明水印
2019/09/06 Python
Python StringIO及BytesIO包使用方法解析
2020/06/15 Python
关于PyCharm安装后修改路径名称使其可重新打开的问题
2020/10/20 Python
python在协程中增加任务实例操作
2021/02/28 Python
Linux文件系统类型
2012/09/16 面试题
表决心的诗句大全
2014/03/11 职场文书
报名委托书
2015/01/29 职场文书
python神经网络学习 使用Keras进行简单分类
2022/05/04 Python