Solved

Execute functions when DOM loaded

Posted on 2008-06-09
4
2,975 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…

689 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