Often, you will want some scripts to run when the page has loaded, even if the images on it haven’t. I wrote up some stuff about this under Best pre-onload.
My full onDOMReady script looks like:
//Main Startup Code function checkDOM(){ if (document.getElementById("footer") || document.getElementById("credit")) Startup(); else setTimeout("checkDOM();",100); } if (document.addEventListener) document.addEventListener("DOMContentLoaded", Startup, null); else setTimeout("checkDOM();",100);
However, Safari will not load pages that use this, since it has document.addEventListener, but does not create DOMContentLoaded events. So, I had to create a nasty little browser check rule:
if (document.addEventListener && (navigator.vendor != "Apple Computer, Inc."))
I’d rather not have to do things like this. Why not?
1 day, 19 hours after the fact.
Thanks Jo, I did find this page: I found that this (general) solution was a bit overkill - and not working well.
The problem was with the
SetInterval()call not being able to be stopped, byClearInterval(), from a function called by theSetInterval()call itself.Thus my script was being run time and time again, and eating up a heap of CPU time. A repeated call to
SetTimeout()rather thanSetInterval()was what I ended up using, as it will not do this.2 days, 3 hours after the fact.
Maybe you could sort this out for me (since being a novice to scripting)
When having lots of images on a page, as I do, I rather would like to load first any scripts before these s load.
My confusion is that I use different ways to load a Js script
first method - inline script:
onload = functionName;second method - external script
// helper function to avoid overwriting any
// window.onload events
function addLoadEvent(func){if(document.getElementById&&document.createTextNode){var oldonload=window.onload;if(typeof window.onload!='function'){window.onload=func;}else{window.onload=function(){oldonload();func();}}}}
// my functions to be loaded
addLoadEvent(FirstFunction);
addLoadEvent(SecondFunction);
addLoadEvent(thirdFunction);
Have any idea how to link eg your method or Dean’s method to my situation?
4 days, 6 hours after the fact.
Hmm. I did write a big comment to respond to this - it seems to have been eaten. I hope it’s not my Catchpa code breaking!
Anyway, include all of the startup code at the end of the document you want to autorun, and then have one function at the top (called Startup, unless you change the startup code), that calls all of the other functions/expressions you want to run.
You may also find some info here about how to include external files - I can’t recall if I’ve posted that or not.
4 days, 14 hours after the fact.
Oh, and you’ll need to modify CheckDOM so it looks for tags with the correct name as your last tag on your page.
4 days, 14 hours after the fact.
You might want to try out my DOMContentLoaded event for browsers. I created a DOMContentLoaded event function array and a DOMContentLoadedInit function that processes the array. To run the DOMContentLoadedInit function I use the DOMContentLoaded event for Mozilla browsers, the defer script attribute that Dean Edwards found for IE and a scheduler function (with code from Brothercake and Simon Willison) in conjuction with onload for all other browsers. You can find this at DOMContentLoaded Event for Browsers.
6 months, 4 weeks after the fact.