(Cross-browser base event/browser detection/Judgment browser) experience code sharing _javascript Tips

Source: Internet
Author: User
cross-browser base events
Copy Code code as follows:

Adding 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 base events
function removeevent (obj, type, fn) {
if (Obj.removeeventlistener) {
Obj.removeeventlistener (Type, FN, false);
else if (obj.detachevent) {
Obj.detachevent (' on ' + type, fn);
}
}
Blocking default behavior across browsers
function Predef (evt) {
var e = evt | | window.event;
if (E.preventdefault) {
E.preventdefault ();
} else {
E.returnvalue = false;
}
}
Get target objects across browsers
function Gettarget (evt) {
if (evt.target) {//W3C
return evt.target;
else if (window.event.srcElement) {//ie
return window.event.srcElement;
}
}
Get character encoding across browsers
function Getcharcode (evt) {
var e = evt | | window.event;
if (typeof E.charcode = = ' number ') {
return e.charcode;
} else {
return e.keycode;
}
}

Browser Detection
Copy Code code 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["$"];
Engine.webkit = parsefloat (engine.ver);
Figure out if it ' s Chrome or Safari
if (/chrome\/(\s+)/.test (UA)) {
Browser.ver = regexp["$"];
Browser.chrome = parsefloat (browser.ver);
else if (/version\/(\s+)/.test (UA)) {
Browser.ver = regexp["$"];
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["$";
engine.khtml = Browser.konq = parsefloat (engine.ver);
else if (/rv: ([^\)]+) \) gecko\/\d{8}/.test (UA)) {
Engine.ver = regexp["$"];
Engine.gecko = parsefloat (engine.ver);
Determine if it ' s Firefox
if (/firefox\/(\s+)/.test (UA)) {
Browser.ver = regexp["$"];
Browser.firefox = parsefloat (browser.ver);
}
else if (/msie ([^;] +)/.test (UA)) {
Engine.ver = Browser.ver = regexp["$";
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 (?:d ows)? ( [^do] {2}) \s? (\d+\.\d+)/.test (UA)) {
if (regexp["$"] = = "NT") {
Switch (regexp["$"]) {
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["$"] = = "9x") {
System.win = "ME";
} else {
System.win = regexp["$"];
}
}
}
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["$");
}
}
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, operating system
Copy Code code as follows:

var browserdetect = {
Init:function () {
This.browser = this.searchstring (this.databrowser) | | "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<data.length;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 (from)
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.