Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>It's not so hard actually - there isn't so many event definitions, only three versions. The first one is the corect one (<code>addEventListener</code>), then there's the IE way (<code>attachEvent</code>) and then there's the compatibility way for older browser (<code>element.onevent = function</code>)</p> <p>So a complete event handling solution would look something like this:</p> <pre><code>setEvent = function(element, eventName, handler){ if('addEventListener' in element){ //W3 element.addEventListener(eventName,handler,false); }else if('attachEvent' in elm){ //IE elm.attachEvent('on'+eventName,handler) }else{ // compatibility elm['on'+eventName] = handler; } } </code></pre> <p>and to clear events:</p> <pre><code>clearEvent = function(element, eventName, handler){ if('removeEventListener' in element){ //W3 element.removeEventListener(eventName,handler,false); }else if('detachEvent' in elm){ //IE elm.detachEvent('on'+eventName,handler) }else{ // compatibility elm['on'+eventName] = null; } } </code></pre> <p>and an example:</p> <pre><code>setEvent(document, "click", function(){alert('hello world!');}); clearEvent(document, "click", function(){alert('hello world!');}); </code></pre> <p>This is not really a complete example though since the compatibility handler always overwrites the previous events (it's not appending actions, it's overwriting) so you probably would like to check if a handler is already set and then save it into some temporary variable and fire it inside the event handler function.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload