(Cross-browser basic events/browser detection/judgment of browsers) experience code sharing _ javascript skills

Source: Internet
Author: User
Some js Code, spare by yourself, master do not laugh at me. (Cross-browser basic events, browser detection, and judgment of the browser name, version number, and operating system) are very practical and convenient for your use. Interested friends can understand, I hope this article will help you. Cross-browser basic events

The Code is as follows:


// Add events across browsers
Function addEvent (obj, type, fn ){
If (obj. addEventListener ){
Obj. addEventListener (type, fn, false );
} Else if (obj. attachEvent ){
Obj. attachEvent ('on' + type, function (){
Fn. call (obj );
});
}
}
// Cross-browser basic events
Function removeEvent (obj, type, fn ){
If (obj. removeEventListener ){
Obj. removeEventListener (type, fn, false );
} Else if (obj. detachEvent ){
Obj. detachEvent ('on' + type, fn );
}
}
// Block default actions across browsers
Function preDef (evt ){
Var e = evt | window. event;
If (e. preventDefault ){
E. preventDefault ();
} Else {
E. returnValue = false;
}
}
// Cross-browser retrieval of target objects
Function getTarget (evt ){
If (evt.tar get) {// W3C
Return evt.tar get;
} Else if (window. event. srcElement) {// IE
Return window. event. srcElement;
}
}
// Cross-browser character encoding
Function getCharCode (evt ){
Var e = evt | window. event;
If (typeof e. charCode = 'number '){
Return e. charCode;
} Else {
Return e. keyCode;
}
}


Browser Detection

The Code is as follows:


Var client = function (){
// Rendering engines
Var engine = {
Ie: 0,
Gecko: 0,
Webkit: 0,
Khtml: 0,
Opera: 0,
// Complete version
Ver: null
};
// Browsers
Var browser = {
// Browsers
Ie: 0,
Firefox: 0,
Safari: 0,
Konq: 0,
Opera: 0,
Chrome: 0,
// Specific version
Ver: null
};
// Platform/device/OS
Var system = {
Win: false,
Mac: false,
X11: false,
// Mobile devices
Iphone: false,
Ipod: false,
Ipad: false,
Ios: false,
Android: false,
NokiaN: false,
WinMobile: false,
// Game systems
Wii: false,
Ps: false
};
// Detect rendering engines/browsers
Var ua = navigator. userAgent;
If (window. opera ){
Engine. ver = browser. ver = window. opera. version ();
Engine. opera = browser. opera = parseFloat (engine. ver );
} Else if (/AppleWebKit \/(\ S +)/. test (ua )){
Engine. ver = RegExp ["$1"];
Engine. webkit = parseFloat (engine. ver );
// Figure out if it's Chrome or Safari
If (/Chrome \/(\ S +)/. test (ua )){
Browser. ver = RegExp ["$1"];
Browser. chrome = parseFloat (browser. ver );
} Else if (/Version \/(\ S +)/. test (ua )){
Browser. ver = RegExp ["$1"];
Browser. safari = parseFloat (browser. ver );
} Else {
// Approximate version
Var safariVersion = 1;
If (engine. webkit <100 ){
SafariVersion = 1;
} Else if (engine. webkit <312 ){
SafariVersion = 1.2;
} Else if (engine. webkit <412 ){
SafariVersion = 1.3;
} Else {
SafariVersion = 2;
}
Browser. safari = browser. ver = safariVersion;
}
} Else if (/KHTML \/(\ S +)/. test (ua) |/Konqueror \/([^;] +)/. test (ua )){
Engine. ver = browser. ver = RegExp ["$1"];
Engine. khtml = browser. konq = parseFloat (engine. ver );
} Else if (/rv :( [^ \)] +) \) Gecko \/\ d {8}/. test (ua )){
Engine. ver = RegExp ["$1"];
Engine. gecko = parseFloat (engine. ver );
// Determine if it's Firefox
If (/Firefox \/(\ S +)/. test (ua )){
Browser. ver = RegExp ["$1"];
Browser. firefox = parseFloat (browser. ver );
}
} Else if (/MSIE ([^;] +)/. test (ua )){
Engine. ver = browser. ver = RegExp ["$1"];
Engine. ie = browser. ie = parseFloat (engine. ver );
}
// Detect browsers
Browser. ie = engine. ie;
Browser. opera = engine. opera;
// Detect platform
Var p = navigator. platform;
System. win = p. indexOf ("Win") = 0;
System. mac = p. indexOf ("Mac") = 0;
System. x11 = (p = "X11") | (p. indexOf ("Linux") = 0 );
// Detect windows operating systems
If (system. win ){
If (/Win (? : Dows )? ([^ Do] {2}) \ s? (\ D + \. \ d + )? /. Test (ua )){
If (RegExp ["$1"] = "NT "){
Switch (RegExp ["$2"]) {
Case "5.0 ":
System. win = "2000 ";
Break;
Case "5.1 ":
System. win = "XP ";
Break;
Case "6.0 ":
System. win = "Vista ";
Break;
Case "6.1 ":
System. win = "7 ";
Break;
Default:
System. win = "NT ";
Break;
}
} Else if (RegExp ["$1"] = "9x "){
System. win = "ME ";
} Else {
System. win = RegExp ["$1"];
}
}
}
// Mobile devices
System. iphone = ua. indexOf ("iPhone")>-1;
System. ipod = ua. indexOf ("iPod")>-1;
System. ipad = ua. indexOf ("iPad")>-1;
System. nokiaN = ua. indexOf ("NokiaN")>-1;
// Windows mobile
If (system. win = "CE "){
System. winMobile = system. win;
} Else if (system. win = "Ph "){
If (/Windows Phone OS (\ d +. \ d +)/. test (ua )){;
System. win = "Phone ";
System. winMobile = parseFloat (RegExp ["$1"]);
}
}
// Determine iOS version
If (system. mac & ua. indexOf ("Mobile")>-1 ){
If (/CPU (? : IPhone )? OS (\ d + _ \ d +)/. test (ua )){
System. ios = parseFloat (RegExp. $1. replace ("_","."));
} Else {
System. ios = 2; // can't really detect-so guess
}
}
// Determine Android version
If (/Android (\ d + \. \ d +)/. test (ua )){
System. android = parseFloat (RegExp. $1 );
}
// Gaming systems
System. wii = ua. indexOf ("Wii")>-1;
System. ps =/playstation/I. test (ua );
// Return it
Return {
Engine: engine,
Browser: browser,
System: system
};
}();


Determine the browser name, version number, and operating system

The Code is as follows:


Var BrowserDetect = {
Init: function (){
This. browser = this. searchString (this. databrown) | "An unknown browser ";
This. version = this. searchVersion (navigator. userAgent)
| This. searchVersion (navigator. appVersion)
| "An unknown version ";
This. OS = this. searchString (this. dataOS) | "an unknown OS ";
},
SearchString: function (data ){
For (var I = 0; I Var dataString = data [I]. string;
Var dataProp = data [I]. prop;
This. versionSearchString = data [I]. versionSearch | data [I]. identity;
If (dataString ){
If (dataString. indexOf (data [I]. subString )! =-1)
Return data [I]. identity;
}
Else if (dataProp)
Return data [I]. identity;
}
},
SearchVersion: function (dataString ){
Var index = dataString. indexOf (this. versionSearchString );
If (index =-1) return;
Return parseFloat (dataString. substring (index + this. versionSearchString. length + 1 ));
},
DataBrowser :[
{
String: navigator. userAgent,
SubString: "Chrome ",
Identity: "Chrome"
},
{String: navigator. userAgent,
SubString: "OmniWeb ",
VersionSearch: "OmniWeb /",
Identity: "OmniWeb"
},
{
String: navigator. vendor,
SubString: "Apple ",
Identity: "Safari ",
VersionSearch: "Version"
},
{
Prop: window. opera,
Identity: "Opera ",
VersionSearch: "Version"
},
{
String: navigator. vendor,
SubString: "iCab ",
Identity: "iCab"
},
{
String: navigator. vendor,
SubString: "KDE ",
Identity: "Konqueror"
},
{
String: navigator. userAgent,
SubString: "Firefox ",
Identity: "Firefox"
},
{
String: navigator. vendor,
SubString: "Camino ",
Identity: "Camino"
},
{// For newer Netscapes (6 +)
String: navigator. userAgent,
SubString: "Netscape ",
Identity: "Netscape"
},
{
String: navigator. userAgent,
SubString: "MSIE ",
Identity: "Internet Explorer ",
VersionSearch: "MSIE"
},
{
String: navigator. userAgent,
SubString: "Gecko ",
Identity: "Mozilla ",
VersionSearch: "rv"
},
{// For older Netscapes (4 -)
String: navigator. userAgent,
SubString: "Mozilla ",
Identity: "Netscape ",
VersionSearch: "Mozilla"
}
],
DataOS :[
{
String: navigator. platform,
SubString: "Win ",
Identity: "Windows"
},
{
String: navigator. platform,
SubString: "Mac ",
Identity: "Mac"
},
{
String: navigator. userAgent,
SubString: "iPhone ",
Identity: "iPhone/iPod"
},
{
String: navigator. platform,
SubString: "Linux ",
Identity: "Linux"
}
]
};
BrowserDetect. init ();
Alert (BrowserDetect. browser); // name
Alert (BrowserDetect. version); // version
Alert (BrowserDetect. OS) // System

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.