File Under: JavaScript

John Resigs addEvent Template

This template was written by John Resig. The original can be found at John Resig’s addEvent function website.

Webmonkey is using it for its JavaScript Events reference page.

What it Looks Like

It’s a blank page, actually. This is meant to be a template for events you add in the HTML and JavaScript portions of the page.

The Code




// addEvent function by John Resig:


function addEvent( obj, type, fn ) {

  if ( obj.attachEvent ) {

    obj['e'+type+fn] = fn;

    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}

    obj.attachEvent( 'on'+type, obj[type+fn] );

  } else

    obj.addEventListener( type, fn, false );





<!-- HTML for example event goes here -->


// Script for example event goes here