×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: Javascript
Posted by: Tobias Richter
Added: Jan 12, 2015 7:39 AM
Modified: Apr 5, 2016 2:52 PM
Views: 90
Tags: javascript
Register event handler cross browsers
  1.     var eventListener = function (element, event, callback) {
  2.         if (element.addEventListener) {
  3.             element.addEventListener(event, callback, false);
  4.         } else {
  5.             element.attachEvent('on' + event, function () {
  6.                 // Set the this pointer same as addEventListener when callback is called
  7.                 return (callback.call(element, window.event));
  8.             });
  9.         }
  10.     }
  11.  
  12. // example use
  13. eventListener(document, 'click', function() {
  14.     // TODO event handler logic
  15. });