Skip to content Skip to sidebar Skip to footer

How To Check If Page Has Fully Loaded(scripts And All)?

I know, there are lots of answered questions about it here. My situation is a bit different, though, and I couldn't find an answer yet. I'm trying to show a message after the page

Solution 1:

Sound like you need to make your code run as a callback to whatever script must execute first, rather than binding it to the document.ready event. If you can post some code, I might be able to help more.

Solution 2:

bianca, why not try to load the message from the script that needs to be run? Trigger the message at the end of the script so you know that the script you need to run completes.

Solution 3:

Javascript executes in the order it is written. I would guess your current code runs after the HTML is loaded (ie. window.onload) if so just have your message be the last thing that is called in your Javascript.

The only exceptions to this is if your doing asynchronously XMLHttpRequest calls then you will want to have have a variable that is set to "false" and when that XMLHttpRequest finishes its sets that variable to "true". Then you can just use setTimeout you listen for when that variable is true.

Solution 4:

window.onload is the solution. window.onload is triggered, when the DOM is ready, and when all of the resources are also loaded into the browser. In other words, it fires when browser's circle stops rotating.

Solution 5:

Making sure a page has truly finished loading isn't always an easy job. especially for pages which rely on content via Ajax, and other assets which load asynchronously (each asset can be loaded independent and in parallel to other assets, like images for example).

My suggestion to approaching such situations:

Create some global Deferred object (a promise) and make sure it will be resolved only after everything that it is depended on is "ready".

Minimal example:

// start your web app code..var p1 = newPromise();
var p2 = newPromise();
var p3 = newPromise();

// when all dependencies are done loading, means the page is truly "ready"Promise.all([p1, p2, p3])
  .then(() => { 
       // page done loading
  });

// somewhere in the code...wait for Ajax content to load// and resolve "p1" after the content has loaded
p1.resolve();

// somewhere in the code...wait for some other Ajax content to load// and resolve "p2" after the content has loaded
p2.resolve();

// somewhere in the code...wait for all the images to load // (http://stackoverflow.com/a/1977898/104380)// and resolve "p3"
p3.resolve();

The point is that you manually need to make sure all the different parts that are async have been fully loaded. it's a tedious work but it's a very solid solution.

Post a Comment for "How To Check If Page Has Fully Loaded(scripts And All)?"