JS Event cross-browser processing scheme

Source: Internet
Author: User

JS Cross browser Event tool:

Event Tool Object

var eventutil = {};

Add Event

Eventutil.addevent = function (element,type,handle) {

if (element.addeventlistener) {//dom Event

Element.addeventlistener (Type,handle,false);

}

else if (element.attachevent) {

Element.attachevent ("on" + Type,handle);

}

else {

Element["on" + type] = handle;

}

}

Delete Event

Eventutil.removeevent = function (element,type,handle) {

if (element.removeeventlistener) {//dom Event

Element.removeeventlistener (Type,handle,false);

}

else if (element.detachevent) {

Element.detachevent ("on" + Type,handle);

}

else {

Element["on" + type] = handle;

}

}

Get Event

Eventutil.getevent = function (event) {

return event?event:window.event;

}

Get the current event owning object

Eventutil.gettarget = function (event) {

return Event.targert | | Event.srcelement;

}

Block Browser Default Events

Eventutil.preventdefault = function (event) {\ \

if (Event.preventdefault) {

Event.preventdefault ();

}

else {

Event.returnvalue = false;

}

}

Block event capture or bubbling

Eventutil.stoppropagation = function (event) {

if (event.stoppropagation) {

Event.stoppropagation ();

}

else {

Event.cancelbubble = false;

}

}

JS Event cross-browser processing scheme

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.