SEARCH
0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Prev | Current Page 82 | Next

Jonathan Snook, Aaron Gustafson, Stuart Langridge, and Dan Webb

"Accelerated DOM Scripting with Ajax, APIs, and Libraries"

The easiest traditional way
was simply to place some JavaScript to run at the very end of the HTML page. Any HTML elements
before the code should be accessible via the script. It isn??™t very unobtrusive, however.
CHAPTER 2 n HTML, CSS, AND JAVASCRIPT 41
The next trick is to use a timer to test for the existence of any elements before using
them and then using window.onload as a fallback. Stuart Colville (http://muffinresearch.
co.uk) did just that with his Element Ready script, which checks to see whether the element
exists. If it doesn??™t, it checks again in a few milliseconds. It continues to check until
the element is found or until the window.onload event fires. A personal variation on his
script is shown here:
var ElementReady={
polled:[], /* store polled elements */
timer:null, /* store timer */
timerStarted: false,
ceasePoll:function()
{
clearTimeout(this.timer);
this.timerStarted = false;
},
startPoll:function()
{
if(!this.timerStarted) this.timer = a
setTimeout(function(){ElementReady.check(false)},100);
},
check:function(clean)
{
for(var i=0;i{
if(document.getElementById(this.polled[i]['element']))
{
this.polled[i]['callback']();
this.polled.splice(i--,1);
}else if(clean){
this.polled.splice(i--,1);
}
}
if(this.polled.length == 0) this.ceasePoll();
},
cleanUp:function()
{
this.check(true);
this.ceasePoll();
},
chkDomId:function(elId,callback) {
var el = document.


Pages:
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94