Solved

Execute functions when DOM loaded

Posted on 2008-06-09
4
2,957 Views
Last Modified: 2012-05-05
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!
0
Comment
Question by:vreten
  • 2
4 Comments
 
LVL 18

Expert Comment

by:ChetOS82
ID: 21745603
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.
0
 
LVL 1

Expert Comment

by:limong
ID: 21746323
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
0
 

Author Comment

by:vreten
ID: 21746475
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

0
 

Accepted Solution

by:
vreten earned 0 total points
ID: 21746762
got the sollution from http://www.brothercake.com/site/resources/scripts/domready/  and with a minor tweek it does exactly what i want.

Thanks!
var rl = new domFunction(functionA, functionB);
 

// DOM scripting by brothercake -- http://www.brothercake.com/

function domFunction(fA, fB, a) {

  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) { fA();fB(); clearInterval(t); }

    }   

    if(n >= 60) clearInterval(t); }, 250);

};

Open in new window

0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now