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 相关文章推荐
js不能跳转到上一页面的问题解决方法
Mar 01 Javascript
JS获取后台Cookies值的小例子
Mar 04 Javascript
JavaScript学习笔记之JS对象
Jan 22 Javascript
JavaScript使用indexOf获得子字符串在字符串中位置的方法
Apr 06 Javascript
JS判断输入字符串长度实例代码(汉字算两个字符,字母数字算一个)
Aug 02 Javascript
vue省市区三联动下拉选择组件的实现
Apr 28 Javascript
基于JavaScript实现评论框展开和隐藏功能
Aug 25 Javascript
基于Vue生产环境部署详解
Sep 15 Javascript
详解如何在React组件“外”使用父组件的Props
Jan 12 Javascript
vue异步加载高德地图的实现
Jun 19 Javascript
浅谈vue-router路由切换 组件重用挖下的坑
Nov 01 Javascript
微信小程序实现自定义动画弹框/提示框的方法实例
Nov 06 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
提问的智慧
2006/10/09 PHP
PHP Global定义全局变量使用说明
2013/08/15 PHP
php堆排序实现原理与应用方法
2015/01/03 PHP
php实现爬取和分析知乎用户数据
2016/01/26 PHP
老生常谈PHP面向对象之标识映射
2017/06/21 PHP
通过JAVASCRIPT读取ASP设定的COOKIE
2006/11/24 Javascript
jQuery 使用手册(四)
2009/09/23 Javascript
JavaScript的public、private和privileged模式
2009/12/28 Javascript
jquery中的sortable排序之后的保存状态的解决方法
2010/01/28 Javascript
js中document.getElementByid、document.all和document.layers区分介绍
2011/12/08 Javascript
JavaScript中this的9种应用场景及三种复合应用场景
2015/09/12 Javascript
JavaScript判断FileUpload控件上传文件类型
2015/09/28 Javascript
解决JS请求服务器gbk文件乱码的问题
2015/10/16 Javascript
JS使用正则表达式找出最长连续子串长度
2017/10/26 Javascript
详解微信小程序实现WebSocket心跳重连
2018/07/31 Javascript
JavaScript引用类型Function实例详解
2018/08/09 Javascript
JavaScript遍历查找数组中最大值与最小值的方法示例
2019/05/24 Javascript
js回溯法计算最佳旅行线路代码实例
2019/09/11 Javascript
vue计算属性无法监听到数组内部变化的解决方案
2019/11/06 Javascript
[33:39]DOTA2上海特级锦标赛C组小组赛#2 LGD VS Newbee第二局
2016/02/27 DOTA
[01:27:30]LGD vs Newbee 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/19 DOTA
Django框架下在URLconf中指定视图缓存的方法
2015/07/23 Python
Python读取和处理文件后缀为.sqlite的数据文件(实例讲解)
2017/06/27 Python
python生成器,可迭代对象,迭代器区别和联系
2018/02/04 Python
python图书管理系统
2020/04/05 Python
Python os模块常用方法和属性总结
2020/02/20 Python
DKNY品牌官网:纽约大都会时尚风格
2016/10/20 全球购物
四方通行旅游网:台湾订房、出国旅游
2017/09/20 全球购物
Theory美国官网:后现代都市风时装品牌
2018/05/09 全球购物
数控加工专业毕业生自荐信
2013/09/27 职场文书
打架检讨书100字
2014/01/19 职场文书
致400米运动员广播稿
2014/02/07 职场文书
集体备课反思
2014/02/12 职场文书
师德承诺书
2015/01/20 职场文书
2015年车间安全管理工作总结
2015/05/13 职场文书
疑《守望先锋2》A测截图泄露 或将推出新模式、新界面
2022/04/03 其他游戏