search
HomeWeb Front-endJS TutorialJavaScript advanced programming reading notes (16) javascript detection browser and operating system-detect.js_javascript skills

Methods of detecting browsers

1. Object feature detection method: a general method to judge browser capabilities. You can use this detection method if you are more concerned about the browser's capabilities than its actual identity. This method is used to create XMLHttpRequest in common native Ajax writing:

Copy code The code is as follows:

IXHR: function(){
if(window.ActiveXObject){
XHR=new ActiveXObject('Microsoft.XMLHTTP');
}else if(window.XMLHttpRequest){
XHR =new XMLHttpRequest();
}else{
return null;
}
}

2. User-agent string detection method: through the browser The user-agent string is parsed to judge. The judgment method is in detect.js below

Detection platform/operating system

It is judged by folding the platform attribute and user-agent string of navigator. The judgment method is in detect.js below

Detect browser and operating system-detect.js
Copy codeThe code is as follows:

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 aVersion2.push("0");
}
} else if (aVersion1.length for (var i=0; i aVersion1.push("0");
}
}

for (var i=0; i
if (aVersion1[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 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;
}
}

作者:Artwl
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
世界上的三大操作系统是什么世界上的三大操作系统是什么Aug 22, 2022 pm 04:24 PM

三大操作系统:1、windows,是微软公司以图形用户界面为基础研发的操作系统,主要运用于计算机、智能手机等设备。2、macOS,是一套由苹果开发的运行于Macintosh系列电脑上的操作系统,是基于XNU混合内核的图形化操作系统。3、linux,是一种免费使用和自由传播的类UNIX操作系统,是一个基于POSIX的多用户、多任务、支持多线程和多CPU的操作系统。

vivo手机是什么系统vivo手机是什么系统Jul 04, 2022 am 11:10 AM

vivo手机是“Funtouch OS”和“OriginOS”系统;2020年11月18日之前,vivo手机搭载的都是“Funtouch OS”系统,2020年11月18日“OriginOS”操作系统发布之后,vivo手机搭载的就是“OriginOS”操作系统了,首款搭载该系统的是“vivo X60”系列手机。

windows操作系统的特点包括什么windows操作系统的特点包括什么Sep 28, 2020 pm 12:02 PM

windows操作系统的特点包括:1、图形界面;直观高效的面向对象的图形用户界面,易学易用。2、多任务;允许用户同时运行多个应用程序,或在一个程序中同时做几件事情。3、即插即用。4、出色的多媒体功能。5、对内存的自动化管理。

什么是闭环控制系统什么是闭环控制系统Jul 04, 2022 pm 04:18 PM

闭环控制系统是控制系统的一种类型,能够把系统输出量的一部分或全部通过一定方法和装置反送回系统的输出端,再将反馈信息与原输入信息进行比较,将比较的结果施加于系统进行控制,避免系统偏离预定目标。

什么是操作系统?它的作用是什么?什么是操作系统?它的作用是什么?Dec 07, 2020 pm 03:04 PM

操作系统是管理计算机硬件与软件资源的计算机程序,是控制和管理计算机软硬件资源,以尽量合理有效的方法组织多个用户共享多种资源的程序集合。操作系统的作用:1、管理系统中的各种资源;2、为用户提供良好的界面。从计算机用户的角度来说,操作系统体现为其提供的各项服务;从程序员的角度来说,其主要是指用户登录的界面或者接口;从设计人员的角度来说,就是指各式各样模块和单元之间的联系。

miui是什么系统miui是什么系统Oct 27, 2022 pm 03:59 PM

miui是小米公司旗下基于Android系统深度优化、定制、开发的第三方手机操作系统。MIUI在Android系统基础上,针对中国用户进行了深度定制,专为小米智能手机设计,包含红米系列,同时支持手机物联。​在隐私方面,miui系统会记录各应用的一切敏感行为,并呈现出来;当应用使用相机、录音、定位权限时,系统会给出明显提示,点击提示即可进行管控。

系统软件中最重要的软件是什么?系统软件中最重要的软件是什么?Dec 10, 2020 pm 04:56 PM

系统软件中最重要的软件是“操作系统”。在计算机中,操作系统是其最基本也是最为重要的基础性系统软件;操作系统需要处理如管理与配置内存、决定系统资源供需的优先次序、控制输入设备与输出设备、操作网络与管理文件系统等基本事务。

电脑开机快慢和什么有关电脑开机快慢和什么有关Aug 12, 2022 am 10:47 AM

影响电脑开机快慢的因素:1、操作系统;如果操作系统太过庞大,开机要加载的文件、服务、软件过多就会让开机速度变慢。2、硬件;硬件对于开机的影响主要是CPU、内存容量和硬盘速度,主板中预存的引导程序会引导CPU通过主板从硬盘中调用启动系统的数据,然后在内存空间内运行,因而CPU、内存大小和硬盘直接影响电脑开机的速度。3、加载项;加载项越多,硬盘要加载的东西就越多,开机速度就越慢。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor