Page is a not externally linkable
- Code, Content, and Presentation
-- JavaScript and AJAX
---- getting a global variable to update every setInterval call


Fotiman - 5:34 pm on May 4, 2012 (gmt 0)


JavaScript has something known as the Conditional Operator [developer.mozilla.org] (aka, the Ternary Operator). The format is:
condition ? expr1 : expr2;

In the example above, window.addEventListener? is looking to see if addEventListener exists for the window object. If browsers have a window object that defines addEventListener, then this will evaluate to a truthy condition, whereby expr1 will be called (window.addEventListener('load',init,false)). Otherwise, expr2 will be called (window.attachEvent('onload',init)).

addEventListener is the "standards" way to add event listers, while attachEvent is IE specific (IE9 added support for addEventListener, but earlier versions used only attachEvent). In other words, if the browser support the standard addEventListeners, use it, otherwise (the browser is old IE), use attachEvent.

If you've been taught to use #3 only, you should complain to your teacher.


Thread source:: http://www.webmasterworld.com/javascript/4449318.htm
Brought to you by WebmasterWorld: http://www.webmasterworld.com