Tuesday, 15 April 2014

dom - how come javascript function has access to the event variable? -



dom - how come javascript function has access to the event variable? -

when javascript function called dom event, within function have access event variable represents dom event? how work behind scene? since function not event argument, , don't utilize argusments object..

a short summary dom events:

the right way bind eventlistener is:

var elem = document.queryselector("div"); elem.addeventlistener("event-type",function(e){ /*e event object*/ });

addeventlistener() listenens event-type event on dom-element elem.

addeventlistener() passes event-object function first parameter. can either access parameter defining in function head (commonly named e)or via arguments[0]. this refers dom-element, event occured.

ie (up version 8) has proprietary way binding event-handlers:

elem.attachevent("event-type",function(){})

it has shortcomings: this refers global object instead of dom-element, , event object not passed parameter.

however, microsoft introduced window.event holds lastly event occured in document. can utilize in function follows:

function(){ var e = window.event; }

this window.event available in several browsers, should not rely on nor utilize (which recommended global variables).

javascript dom

No comments:

Post a Comment