getElementById("__ie_onload");
script.onreadystatechange = function() {
if (this.readyState == "complete") {
init(); // call the onload handler
}
};
/*@end @*/
// for Safari
if (/WebKit/i.test(navigator.userAgent)) { // sniff
var _timer = setInterval(function() {
if (/loaded|complete/.test(document.readyState)) {
CHAPTER 2 n HTML, CSS, AND JAVASCRIPT 43
clearInterval(_timer);
init(); // call the onload handler
}
}, 10);
}
// for other browsers
window.onload = init;
function init() {
// quit if this function has already been called
if (arguments.callee.done) return;
// flag this function so we don't do the same thing twice
arguments.callee.done = true;
// do stuff
};
In Dean??™s code, the init() function is the only function that gets called. This differs from
the previous approach in that instead of seeing whether the specific element you want exists
and is ready for you, you instead see whether the entire document is ready for you. For the
most part, you rely on the browser to tell you that it has loaded, whereas the previous script
continually checked to see whether that is the case.
Attaching Events Using DOM Methods
So far, you??™ve seen the use of attaching event handlers inline and using object properties.
Inline handlers are difficult for keeping things separated, and attaching via object properties
means you can attach only one handler at a time. To get around this, there is a DOM method
that enables multiple event handlers to be added to a single object: addEventListener.
Pages:
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96