How to execute multiple processing procedures in Javascript

Source: Internet
Author: User
When I used to write JavaScript scripts, all events used Object. event = handler; . This method is common for Internet Explorer, Mozilla/Firefox, and opera. However, one problem is that this method only corresponds to one event processing process. If you want an event to be able to execute multiple processing procedures in sequence, it is not easy to use.

however, Internet Explorer provides an attachevent method from 5.0. With this method, you can assign multiple processing processes to an event. Attachevent is also applicable to opera. But the problem is that Mozilla/Firefox does not support this method. However, it supports another addeventlistener method, which is similar to attachevent and is used to assign multiple processing processes to an event. However, there are some differences between the events they assign. In the attachevent method, the events start with "on", and in the addeventlistener, the "on" with no start with the event ", in addition, addeventlistener has a third parameter. Generally, this parameter can be set to false.
therefore, if you want to assign multiple processing processes to an event in your Program , you only need to judge the browser first, select attachevent or addeventlistener based on different browsers. Example:
If (document. all) {
window. attachevent ('onload', handler1);
window. attachevent ('onload', handler2);
}< br> else {
window. addeventlistener ('load', handler1, false);
window. addeventlistener ('load', handler2, false);
}< br>

note: the execution sequence of multiple processes assigned by attachevent is random, so there should be no sequential dependency between these processes. In addition, attachevent and addeventlistener are not only applicable to window objects, but also some other objects support this method.

for convenience, I also wrote a function sunet_addevent (OBJ, evtype, FN), as follows:

function sunet_addevent (OBJ, evtype, FN)
{< br> If (obj. addeventlistener) {
obj. addeventlistener (evtype, FN, false);
return true;
}else if (obj. attachevent) {
var r = obj. attachevent ("On" + evtype, FN);
return r;
}else {
return false;
}< BR >}< br> // call example: sunet_addevent (window, "locad", init);

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.