daily pastebin goal
44%
SHARE
TWEET

Untitled

a guest Oct 19th, 2018 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Cross-browser event object
  3.  * Adapted from Professional JavaScript for Web Developers, 3rd Edition, by Nicholas C. Zakas
  4.  *
  5.  * Usage example:
  6.  *
  7.  *     eventUtil.addHandler(elm, "click", function (e) {
  8.  *         e = eventUtil.getEvent(e);    // Get event object
  9.  *         eventUtil.preventDefault(e);  // Prevent default action
  10.  *     });
  11.  *
  12.  */
  13.  
  14. (function (window) {
  15.  
  16.     'use strict';
  17.  
  18.     var eventUtil = {
  19.  
  20.         addHandler: function (element, type, handler) {
  21.             if (element.addEventListener) { // W3C
  22.                 element.addEventListener(type, handler, false);
  23.             } else if (element.attachEvent) { // IE
  24.                 element.attachEvent("on" + type, handler);
  25.             } else {
  26.                 element["on" + type] = handler;
  27.             }
  28.         },
  29.  
  30.         removeHandler: function (element, type, handler) {
  31.             if (element.removeEventListener) {
  32.                 element.removeEventListener(type, handler, false);
  33.             } else if (element.detachEvent) {
  34.                 element.detachEvent("on" + type, handler);
  35.             } else {
  36.                 element["on" + type] = null;
  37.             }
  38.         },
  39.  
  40.         // Get event element
  41.         getEvent: function (e) {
  42.             return e || window.event; // Dom event object || IE event object
  43.         },
  44.  
  45.         // Get source element
  46.         getTarget: function (e) {
  47.             return e.target || e.srcElement; // Dom event object || IE event object
  48.         },
  49.  
  50.         // Prevent default action
  51.         preventDefault: function (e) {
  52.             if (e.preventDefault) { // Dom event object
  53.                 e.preventDefault();
  54.             } else { // IE event object
  55.                 e.returnValue = false;
  56.             }
  57.         },
  58.  
  59.         // Cancel event bubbling
  60.         stopPropagation: function (e) {
  61.             if (e.stopPropagation) { // Dom event object
  62.                 e.stopPropagation();
  63.             } else { // IE event object
  64.                 e.cancelBubble = true;
  65.             }
  66.         }
  67.     };
  68.  
  69. }(window));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top