A true example of the difference between attachevent and AddEventListener

Source: Internet
Author: User

Recently encountered Attachevent and AddEventListener two events, yo, decisive study ~ ~

First to popularize the basic knowledge:

Attachevent and AddEventListener: Attach other processing events to an event.

Online is generally said attachevent for non-Mozilla series, AddEventListener for the Mozilla series.

(Mozilla series in the browser, that is, the Firefox browser, I think it can also refer to the series of browsers supporting the standard)

In theory, you have to try it yourself!

The following code was written in conjunction with the lookup material to test:

<! DOCTYPE html>varBtn1obj = document.getElementById ("BTN1"); functionmethod1 () {alert ("Method1"); }    functionmethod2 () {alert ("Method2"); }    functionmethod3 () {alert ("Method3"); }    functionAttacheventlisten (obj,e,fun) {/*obj.attachevent obj.attachevent ("on" +e,fun): Obj.addeventlistener (e,fun,false);*/Obj.attachevent ("On" +e,fun);//Test Browser, found: Firefox, Chrome, IE9, IE10, IE11, Safari, Opera are incompatible, IE7, IE8 compatible,        //Obj.addeventlistener (E,fun,false);//test browser, found: Firefox, Chrome, IE, Safari, opera are compatible, IE7, IE8 incompatible,} attacheventlisten (Btn1obj,"Click", METHOD1); Attacheventlisten (Btn1obj,"Click", METHOD2); Attacheventlisten (Btn1obj,"Click", method3);</script>

The run result comment is in the code above. (only major browsers were tested: Ie7-11, Firefox, Chrome, Opera, Safari)

To sum up is:

attachevent--compatible: IE7, IE8, not compatible with Firefox, Chrome, IE9, IE10, IE11, Safari, Opera

addeventlistener--compatible: Firefox, Chrome, IE, Safari, Opera; incompatible IE7, IE8

It can also be seen from the beginning of IE9, many JS has been in accordance with the standards of the Web, other mainstream browsers are also in line with the standard.

A true example of the difference between attachevent and AddEventListener

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.