JavaScript compatible with Add (cancel) Event monitoring method for Internet Explorer

Source: Internet
Author: User

As a necessary feature of JavaScript itself, the event is ubiquitous in the current use of JavaScript, and JavaScript events are used when writing to JAVASCRPT. The following is a JavaScript in the addition of event monitoring and cancellation of event monitoring methods, of course, to be compatible with the Internet and ie. The following are the compatible codes:

[JavaScript]View PlainCopy 
  1. Adding an event listener compatibility function
  2. function AddHandler (target, EventType, handler) {
  3. if (target.addeventlistener) {//mainstream browser
  4. AddHandler = function (target, EventType, handler) {
  5. Target.addeventlistener (EventType, Handler, false);
  6. };
  7. }else{//ie
  8. AddHandler = function (target, EventType, handler) {
  9. Target.attachevent ("On" +eventtype, Handler);
  10. };
  11. }
  12. //execute a new function
  13. AddHandler (target, EventType, handler);
  14. }
  15. Delete Event listener compatibility function
  16. function RemoveHandler (target, EventType, handler) {
  17. if (target.removeeventlistener) {//mainstream browser
  18. RemoveHandler = function (target, EventType, handler) {
  19. Target.removeeventlistener (EventType, Handler, false);
  20. }
  21. }else{//ie
  22. RemoveHandler = function (target, EventType, handler) {
  23. Target.detachevent ("On" +eventtype, Handler);
  24. }
  25. }
  26. //execute a new function
  27. RemoveHandler (target, EventType, handler);
  28. }

The above code is to listen to the event function is optimized, delay loading event listener function, so that you do not have to judge the addition or deletion of events every time, only the first time to add or delete the listener event when the judgment, will greatly improve the performance of the monitoring event.

JavaScript compatible with Add (cancel) Event monitoring method for Internet Explorer

Related Article

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.