Attachevent and AddEventListener compatibility

Source: Internet
Author: User

When you write JavaScript scripts before, events are implemented using the
Object.event = handler;
The way you initialize it. This approach is common to Internet Explorer, Mozilla/firefox, and Opera. One problem, however, is that this approach can only have one event for an event-handling process. It is not good if you want an event to execute multiple processes sequentially.

However, Internet Explorer provides a attachevent method starting at 5.0, which allows you to assign multiple processes to an event. Attachevent is also suitable for the current Opera. But the problem is that Mozilla/firefox does not support this approach. But it supports another AddEventListener method, which is similar to attachevent and is used to assign multiple processes to an event. But the events they assign are somewhat different, in the Attachevent method, the event starts with "on," and in AddEventListener, the event has no "on" at the beginning, and the other AddEventListener has a third parameter, which is typically specified as False is OK.

So if you want to assign multiple processes to an event in your program, just judge the browser first, and then choose to use Attachevent or AddEventListener on a different browser. Examples are as follows:

if (window.attachevent) {
Window.attachevent ("onload", remove);
else if (Window.addeventlistener) {
Window.addeventlistener ("Load", remove, false);
}
function Remove () {
var div = document.getelementbyidx_x ("Divprogressbar");
Document.body.removeChild (DIV);
}

============= or you can use it.

if (document.all) {window.attachevent (' onload ', remove)}//ie
Else{window.addeventlistener (' Load ', remove,false);}//firefox

Note: The sequence of execution of several procedures assigned by attachevent is random, so there is no sequential dependency between these processes. In addition, attachevent and AddEventListener not only apply to window objects, but other objects also support the method. The highest level of programmer: static paralysis, if epilepsy


Original address: https://www.cnblogs.com/demonxian3/p/6374388.html


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.