Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Execute functions when DOM loaded

Posted on 2008-06-09
4
Medium Priority
?
2,987 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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 …
In this blog, we’ll look at how improvements to Percona XtraDB Cluster improved IST performance.
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…

636 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