Link to home
Start Free TrialLog in
Avatar of vreten
vreten

asked on

Execute functions when DOM loaded

Hi, I have a couple of function that I want to execute when the DOM is fully loaded (I.e. when all the link elements are loaded).
I have tried
window.onload = functionA();
window.onload = functionB();
But that is not good enough because, for example, if the page contains heavy elements such as large images the functions will not be triggered until the whole page is fully rendered.

Is there a way to check when the DOM has loaded all the links in an document and then trigger my functions?

Thanks!
Avatar of ChetOS82
ChetOS82
Flag of United States of America image

I have only ever seen this done by adding an onload even to the long-loading images and having them set their status in a global list.  Once all the list elements have been set to loaded, then I continue processing.
Hello Expert friend.

I think, if you define and even call the function on the last lines after the closing body tag (</body>) you can be sure that this function will be executed after all the dom is writed

and if you are calling functions that take time to load elements such as dynamic "select-one" maybe you should place the callin to this wanted "last function" at the end of the last dynamic one

:) good luck
Avatar of vreten
vreten

ASKER

I am looking for something in line with the code attached.

As I understand the onload executes when the page is "ready and loaded" but even if the page should not ever load 100% the DOM tree is loaded. I want to trigger my functions when the DOM is ready and does not really care about when the page i ready.

And  you might say "why not use the attached code.." ... the problem is that is doesn't work :) At least not i FF who complains about:
"uncaught exception: [Exception... "Out of Memory" nsresult: "0x8007000e (NS_ERROR_OUT_OF_MEMORY)"
var x = new isDomReady(myFunction();
 
function isDomReady(f, x) {
  var n = 0;
  var t = setInterval(function() {
    var c = true;
    n++;
    if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null)) {
      c = false;
      if(typeof a == 'object') {
        for(var i in a) {
          if ((a[i] == 'id' && document.getElementById(i) == null)||(a[i] == 'tag' && document.getElementsByTagName(i).length < 1)) { c = true; break; }
        }
      }
      if(!c) { f(); clearInterval(t); }
    }   
    if(n >= 60) clearInterval(t); }, 250);
};

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of vreten
vreten

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial