1

My requirement is, I have to execute a script after all the page scripts execution is done. I am using the below code to do the same

$(window).load(function ()
{
  var j = setTimeout (function ()
  {
     alert("All page scripts have been executed and DOM is ready.");
     // And here the script will work 
        to update the DOM changes made by the pagescripts.
  },100); // Here execution time cant be less than 100 all the time, right?
});

But the problem is, In the above case I have used time 100. But sometimes the page is being loaded in 101 milliseconds. In such cases the script is failed to update the changes. Lets see an example for this

There is a page script which will add images to the DOM when DOM is ready. And if I use

$(document).ready()   

the script is failed to detect images as the images are not yet added to the DOM.

Please let me know if you need some more clarification on Question.

Exception
  • 8,111
  • 22
  • 85
  • 136
  • 1
    possible duplicate of [Delaying a jquery script until everything else has loaded](http://stackoverflow.com/questions/1012140/delaying-a-jquery-script-until-everything-else-has-loaded) – Manse Apr 11 '12 at 13:49
  • Look at the top answer of the duplicate - it tells you the order things run ... so just make sure your `$(window).load()` is at the bottom of the body - it will be run once all scripts within `$(document).ready()` and all images and everything is executed – Manse Apr 11 '12 at 13:51
  • @ManseUK Do you mean, should I paste the code at the end of the page after the page scripts..? – Exception Apr 11 '12 at 14:09
  • The answer to that duplicate question gives you a detailed explanation but essentially yes - within the `$(window).load()` wrapper – Manse Apr 11 '12 at 14:11

1 Answers1

0

If the problem are the images you need to preload them.
Try using this extension.

Chalda Pnuzig
  • 406
  • 4
  • 16