JavaScript Events - Part 1 - Setting Events
Three Event Addition Methods
- In HTML as in <a onclick="myfunc">.
- As an Attribute as in element.onclick = myfunc;
- Using the DOM Model as in element.addEventListener( "click", myfunc, false);.
The HTML Event
You can set an event for an element directly in the HTML using the event name preceded by "on". The code generates a function and compiles the attribute value text as the function body. This means that you are not running in the same context as when you programatically attach an event, since your code is running inside a method body. One browser inconsistency is overcome by this method: IE generates function(){yourcode} while Mozilla generates function(event){yourcode}. This allows both event types to access the event object as event even though it is passed in in Mozilla and is a global for IE. Warning: Don't use this to reference the element firing the event. It works for Mozilla, but not for IE.Event Attribute
In the pre-DOM world, attributes were just fields on the HTML element. For backwards compatability they still are and always will be (for HTML anyway). So,
element.onclick = function() { alert( "click"); }
will work. The problem is that a single element can only have one event function per event type. This makes it difficult to produce library type code for JavaScript.
DOM Events
If events are set using the addEventListener() method they are stacked and all events added are fired. Perfect, except that IE (as of 6) does not support this part of the DOM.Summary
So, what are the problems?- Event setting that's portable across browsers does not allow more than one event per type per element.
- addEventListener() is not platform independent.








2 Comments:
Hmmm… an effective technology comes with a befitting name. ‘Agile’ as they say it. The long and abrupt, turns and twists in the alley of Software Development process now face the inevitable. Welcome to Agile Software Development methodology. With this, the Software Development process would now hopefully be more structured, less convulsive (for its developers of course!) and markedly disciplined.
thank you for this part it is very useful for me..
Anna Kolesnik
[ http://www.helion-prime.com ]
Post a Comment
<< Home