Solved

Execute functions when DOM loaded

Posted on 2008-06-09
4
2,969 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 Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
HTML5 Looping table certain Cell 11 54
What does GoogleTagMgr javascripts below do 5 29
Dynamic Dropdowns 15 32
Filktering Alphabetically 8 29
The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
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…

809 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