Home >Web Front-end >JS Tutorial >Implementation code for using jQuery plug-in extension to identify the type and version of browser kernel and shell_jquery

Implementation code for using jQuery plug-in extension to identify the type and version of browser kernel and shell_jquery

WBOY
WBOYOriginal
2016-05-16 18:00:301006browse

Especially nowadays, various browsers are flying everywhere (it is said that there are as many as 200 browsers with IE as the core).
Today I wrote a plug-in extension based on jQuery, which is mainly used to identify the type and version of the browser kernel and shell. It can identify the kernels of various browsers and already supports a variety of domestic mainstream browsers.
Without further ado, here’s my JavaScript code: (File name: jquery.browsertype-1.0.js)

Copy code The code is as follows:

/**
* jQuery plug-in development method two: Step 1: Plug-in definition
*/
jQuery.myPlugin = {
//Get the type and version of the browser’s kernel and shell
Client : function (){
//Browser kernel type (only five types)
var engine = { ie:0, webkit:0, gecko:0, opera:0, khtml:0 };
//Browser shell type (common domestic browsers include: 360 Browser, Maxthon, Tencent QQTT Browser, Window of the World, Comet Browser, Green Browser, Traditional IE, Google Chrome, Netscape, Firefox, Opera , Apple Safari, etc.)
var shell = { se360:0, se:0, maxthon:0, qq:0, tt:0, theworld:0, cometbrowser:0, greenbrowser:0, ie:0, chrome :0, netscape:0, firefox:0, opera:0, safari:0, konq:0 };
//Get client browser information
var ua = navigator.userAgent.toLowerCase();
for (var type in engine) {
if (typeof type === 'string') {
var regexp = 'gecko' === type ? /rv:([w.] )/ : RegExp(type '[ \/]([\w.] )');
if (regexp.test(ua)){
engine.version = window.opera ? window.opera.version() : RegExp.$1;//Browser kernel version
engine[type] = parseFloat(engine.version);
engine.type = type;//Browser kernel type
break;
}
}
}
for (var type in shell) {
if (typeof type === 'string') {
var regexp = null;
switch(type) {
case "se360": regexp = /360se(?:[ /]([w.] ))?/; break;
case "se": regexp = /se ([w.] )/; break;
case "qq": regexp = /qqbrowser/([w.] )/; break;
case "tt": regexp = /tencenttraveler ([w.] )/; break;
case "safari": regexp = /version/([w.] )/; break;
case "konq": regexp = /konqueror/([w.] )/; break;
case "netscape" : regexp = /navigator/([w.] )/; break;
default: regexp = RegExp(type '(?:[ \/]([\w.] ))?');
}
if (regexp.test(ua)) {
shell.version = window.opera ? window.opera.version() : RegExp.$1 ? RegExp.$1 : 'unknown';//Browser shell version
shell[type] = parseFloat(shell.version);
shell.type = type;//Browser shell type
break;
}
}
}
//Return the type and version of the browser kernel and shell: engine is the kernel, shell is the shell
return { engine: engine, shell: shell };
}
};
/**
* jQuery plug-in development method two: Step 2: Execute the plug-in
* jQuery.myBrowser plug-in: obtain the type and version of the browser's kernel and shell
* Usage example:
* (1) Browser kernel: alert("Your browser information is as follows: nkernel type: " jQuery.myBrowser.engine.type ", kernel version: " jQuery.myBrowser.engine.version);
* (2) Browser Shell: alert("Your browser information is as follows: shell type: " jQuery.myBrowser.shell.type ", shell version: " jQuery.myBrowser.shell.version);
*/
jQuery.myBrowser = jQuery.myPlugin.Client();

Usage example: (test.html)
Copy code The code is as follows:




Get browser information

< script type="text/javascript">
google.load("jquery", "1.4.2");
google.load("jqueryui", "1.7.2");
< ;/script>





Test the type and version of the browser's kernel and shell:



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