Best pre-onload
-
Comments:
- here.
I’ve been using domFunction()
to determine whether it’s safe to do all of my DOM modification code, but with some testing, I noticed that some pages were causing Firefox to eat up huge amounts of CPU time. I tracked the error down to the domFunction call, as it uses setInterval()
to run a function every n seconds. This in itself is nice to know (I was looking for a similar feature a couple of days ago), but the nice part is that it has a stopping function: clearInterval()
. You are supposed to use it like this:
timer = setInterval("Function();", interval);
This will then call the function Function every interval milliseconds. Neato. Except I only want a particular thing to happen once. So, as a part of my code, I was calling clearInterval(timer)
inside a function called by Function.
function Startup(){
Gravatars();
.
.
.
HumanTimePosts();
clearInterval(timer);
}
//Code for all functions in here.
timer = setInterval(function(){
if (document.getElementById(\"footer\")){
Startup();
}
}, 1000);
Which should work. Except for some reason, calls to clearInterval cannot come from inside functions called by setInterval. Or something like that. My next solution was to use:
function checkDOM(){
if (getById("footer")||getById("credit")) Startup();
else setTimeout("checkDOM();",100);
}
setTimeout("checkDOM();"
Which solves the problem, but it ugly. Finally, I came across this little number:
if (document.addEventListener) {
document.addEventListener("DOMContentLoaded", Startup, null);
}
This only works with Mozilla browsers, so for the time being I’ve combined the last two.