DOM (14): Agent Detection and event handling (cross-browser)

Source: Internet
Author: User

First, testing

For user agent detection, detection range includes browser engine, platform, Windows, mobile device and game system, etc.

/** User Agent detection script, detection range including browser engine, platform, Windows, mobile device and game system*/varClient =function(){//Rendering engine    varEngine = {ie:0,//iegecko:0,//Firefoxwebkit:0,//chrome and Safarikhtml:0,//linux under the Konqueroropera:0,//operaVerNULL      //Full version number};//Browser    varBrowser = {ie:0, firefox:0, safari:0, konq:0, opera:0, chrome:0, ver:NULL        //Specific version number};//platforms, devices and operating systems    varSystem = {//Operating systemWinfalseMac:false, XLL:false,//unix        //Mobile devicesIphone:falseIpod:falseIpad:falseIos:falseAndroid:false, Nokian:false,//NokiaWinMobile:false,//windows Mobile        //game systemWii:false,//wiiPs:false            //playstation 3};//detection rendering engine and browser    varUA = 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);//Are you sure 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 number            varSafariversion = 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 Firefox is not        if(/firefox\/(\s+)/test (UA)) {Browser.ver = regexp["$"];        Browser.firefox = parsefloat (engine.ver); }    }Else if(/mise ([^;] +)/.test (UA)) {engine.ver = Browser.ver = regexp["$"];    engine.ie = browser.ie = parsefloat (engine.ver); }//Detection browserbrowser.ie = engine.ie; Browser.opera = Engine.opera;//Detection platform    varp = navigator.platform; System.win = P.indexof ("Win") = = 0; System.mac = P.indexof ("Mac") = = 0; System.xll = (P = ="XLL") || (P.indexof ("Linux") = = 0);//Detect Windows operating system    if(System.win) {if(/win (?:d ows)? ([^ Do]{2}) \s? (\d+\.\d+)?/.test (UA)) {if(regexp["$"] =="NT")            {Switch(regexp["$"])                { Case "5.0": System.win =" the"; 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 devicesSystem.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["$"]); }    }//Detect 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; }    }//Detect Android    if(/android (\d+\.\d+)/.test (UA))    {system.android = parsefloat (regexp.$1); }//game systemSystem.wii = Ua.indexof ("Wii") >-1; system.ps =/playstation/i.test (UA);//Return results    return{engine:engine, Browser:browser, System:system};} ();

Second, cross-browser processing of events

/* * parameter:ele--> element,type--> event type, without on,handler--> event handler */ varEventHandler = {AddHandler:function(Ele,type,handler) {//Add handler//DOM2 level event handling if(Ele.addeventlistener) {Ele.addeventlistener (Type,handler,false); }Else if(ele.attachevent) {Ele.attachevent ("on"+type,handler); }Else{//DOM0 level event handlingele["on"+type] = handler; }}, RemoveHandler:function(Ele,type,handler) {//delete handlerif(Ele.removeeventlistener) {Ele.removeeventlistener (Type,handler,false); }Else if(ele.detachevent) {Ele.detachevent ("on"+type,handler); }Else{ele["on"+type] =NULL; } },

Getevent:function (Event) {//Get Event Object

return event?event:window.event;

},

Gettarget:function (Event) {//Get event Target

return Event.target | | Event.srcelement;

},

Preventdefault:function (Event) {//block event default behavior

if (Event.preventdefault) {

Event.preventdefault ();

}else{

Event.returnvalue = false;

}

},

Stoppropagation:function (Event) {//block event stream

if (event.stoppropagation) {

Event.stoppropagation ();

}else{

Event.cancelbubble = true;

}

},

Getrelatedtarget:function (Event) {//Get related elements in MouseOver and mouseout

if (event.relatedtarget) {

return event.relatedtarget;

The Relatedtarget attribute is not supported for}else if (event.toelement) {//IE8 and the following versions

return event.toelement;

}else if (event.fromelement) {

return event.fromelement;

}else{

return null;

}

},

Getwheeldelta:function (Event) {//Get mouse wheel data

if (Event.wheeldelta) {

Return (Client.engine.opera && Client.engine.opera < 9.5?-event.wheeldelta:event.wheeldelta);

}else{

return–event.detail*40; For Firefox

}

},

Getcharcode:function (Event) {//Get character encoding

if (Event.charcode = = "Number") {

return event.charcode;

}else{

return event.keycode;

}

}, };

Please forgive me for any deficiencies.

Original starting: http://www.ido321.com/1403.html

DOM (14): Agent Detection and event handling (cross-browser)

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.