views:

10965

answers:

5

I have a script that is being inserted dynamically via another script. The code in that script is wrapped inside the $(window).load() event because it requires the images on the page to have all loaded. In some browsers it works fine, but in others it seems not to fire because the page has already finished loading by the time the code is run.

Is there any way to check and see if the page has already finished loading - either via jQuery or JavaScript? (including images)

In this situation, I don't have access to the onload event of the original document (aside from altering it via the loaded script - but that would seem to present the same problem).

Any ideas/solutions/advice would be greatly appreciated!

+1  A: 

I think your problem would resolve itself if you'd use $(document).ready instead of $(window).load - see the jquery documentation.

TML
Just a snippet from the docs linked:In a nutshell, this is a solid replacement for using window.onload...your bound function will be called the instant the DOM is ready to be read and manipulated, which is when 99.99% of all JavaScript code needs to run.
TML
$(document).ready fires if the DOM in place but doesn't wait for the images to finish loading (if I'm not mistaken). In this case, I need the images to have loaded so I can read their attributes.
DismissedAsDrone
A: 

Don't know if this is what you are after, but have you tried(?):

$(document).ready(function(){
    ...
});

http://docs.jquery.com/Events/ready#fn

GreenieMeanie
+1  A: 

You could try setting up a handler that's invoked via a timeout that will check the images to see if their properties are available. Clear the timer in the load event handler so if the load event occurs first, the timer won't fire. If the properties aren't available, then the load event hasn't fired yet and you know that your handler will eventually be invoked. If they are, then you know that the load event occurred before your handler was set and you can simply proceed.

Pseudocode

 var timer = null;
 $(function() {
    $(window).load( function() {
        if (timer) {
           clearTimeout(timer);
           timer = null;
        }
        process();
    });
    timer = setTimeout( function() {
        if (checkAvailable())
           process();
        }
    }, 10*1000 ); // waits 10 seconds before checking
 });

 function checkAvailable()
 {
     var available = true;
     $('img').each( function() {
         try {
             if (this.height == 0) {
                available = false;
                return false;
             }
         }
         catch (e) {
             available = false;
             return false;
         }
      });
      return available;
  }

  function process() {
      ... do the real work here
  }
tvanfosson
This is accomplishes pretty much what I was looking to do. Just tweaked it a bit to fire the timer at short intervals instead of waiting 10 seconds. Thanks!
DismissedAsDrone
+1  A: 

I wrote a plugin that may be of some use: http://plugins.jquery.com/project/window-loaded

Stew