Solved

Execute functions when DOM loaded

Posted on 2008-06-09
4
2,964 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sorting a string 1 29
Jquery GMAP3 Hierarchy 1 26
javascript: add id amounts 5 47
Hide cell in a table 2 10
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…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

776 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