JavaScript Browser compatibility method

Source: Internet
Author: User

var eventutil = {

Addhandler:function (element, type, handler) {
if (Element.addeventlistener) {
Element.addeventlistener (type, handler, false);
} else if (element.attachevent) {
Element.attachevent ("On" + type, handler);
} else {
Element["on" + type] = handler;
}
},

Getbutton:function (event) {
if (Document.implementation.hasFeature ("Mouseevents", "2.0")) {
return Event.button;
} else {
Switch (Event.button) {
Case 0:
Case 1:
Case 3:
Case 5:
Case 7:
return 0;
Case 2:
Case 6:
return 2;
Case 4:return 1;
}
}
},

Getcharcode:function (event) {
if (typeof Event.charcode = = "Number") {
return event.charcode;
} else {
return event.keycode;
}
},

Getclipboardtext:function (event) {
var clipboarddata = (Event.clipboarddata | | window.clipboarddata);
Return Clipboarddata.getdata ("text");
},

Getevent:function (event) {
Return event? Event:window.event;
},

Getrelatedtarget:function (event) {
if (event.relatedtarget) {
return event.relatedtarget;
} else if (event.toelement) {
return event.toelement;
} else if (event.fromelement) {
return event.fromelement;
} else {
return null;
}

},

Gettarget:function (event) {
return Event.target | | Event.srcelement;
},

Getwheeldelta:function (event) {
if (Event.wheeldelta) {
Return (Client.engine.opera && Client.engine.opera < 9.5-event.wheeldelta:event.wheeldelta);
} else {
Return-event.detail * 40;
}
},

Preventdefault:function (event) {
if (Event.preventdefault) {
Event.preventdefault ();
} else {
Event.returnvalue = false;
}
},

Removehandler:function (element, type, handler) {
if (Element.removeeventlistener) {
Element.removeeventlistener (type, handler, false);
} else if (element.detachevent) {
Element.detachevent ("On" + type, handler);
} else {
Element["on" + type] = NULL;
}
},

Setclipboardtext:function (event, value) {
if (event.clipboarddata) {
Event.clipboardData.setData ("Text/plain", value);
} else if (Window.clipboarddata) {
Window.clipboardData.setData ("text", value);
}
},

Stoppropagation:function (event) {
if (event.stoppropagation) {
Event.stoppropagation ();
} else {
Event.cancelbubble = true;
}
}

};

JavaScript Browser compatibility method

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.