Monitoring and unblocking of different browsers in JS event model reference

Source: Internet
Author: User

/eventutil.js var eventutil = new Object;
/*
This method is used to add an event to a specific object, Otarget is the specified object, Seventtype is the event type, such as Click, KeyDown, and so on, Fnhandler is the event callback function
/*
Eventutil.addeventhandler = function (Otarget, Seventtype, Fnhandler) {
Firefox case
if (Otarget.addeventlistener) {
Otarget.addeventlistener (Seventtype, Fnhandler, false);
}
IE under
else if (otarget.attachevent) {
Otarget.attachevent ("on" + Seventtype, Fnhandler);
}
else {
Otarget["on" + seventtype] = Fnhandler;
}
};
/*
This method is used to remove a specific event for a particular object, Otarget is the specified object, Seventtype is the event type, such as Click, KeyDown, and so on, Fnhandler is the event callback function
/*
Eventutil.removeeventhandler = function (Otarget, Seventtype, Fnhandler) {
if (Otarget.removeeventlistener) {
Otarget.removeeventlistener (Seventtype, Fnhandler, false);
else if (otarget.detachevent) {
Otarget.detachevent ("on" + Seventtype, Fnhandler);
} else {
Otarget["on" + seventtype] = null;
}
};

/*
Format the event, which provides a consistent event by using this method because of the different ways that IE and other browsers can get events and the properties of events
*/
Eventutil.formatevent = function (oevent) {
Isie and Iswin refer to a JS file to determine the browser and operating system type
if (Isie && iswin) {
Oevent.charcode = (Oevent.type = = "KeyPress")? oevent.keycode:0;
IE only supports bubbling and does not support capture
Oevent.eventphase = 2;
Oevent.ischar = (Oevent.charcode > 0);
Oevent.pagex = Oevent.clientx + document.body.scrollLeft;
Oevent.pagey = Oevent.clienty + document.body.scrollTop;
To block the default behavior of an event
Oevent.preventdefault = function () {
This.returnvalue = false;
};

Convert Toelement,fromelement to Standard Relatedtarget
if (Oevent.type = = "Mouseout") {
Oevent.relatedtarget = oevent.toelement;
else if (Oevent.type = = "MouseOver") {
Oevent.relatedtarget = oevent.fromelement;
}
Cancel bubbling
Oevent.stoppropagation = function () {
This.cancelbubble = true;
};

Oevent.target = oevent.srcelement;
Add event occurrence time attribute, ie does not
Oevent.time = (new Date). GetTime ();
}
return oevent;
} ;

Eventutil.getevent = function () {
if (WI

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.