Hamed Moayeri
Hamed Moayeri

Reputation: 83

How to determind if the web page is loaded completely in web browser?

I am working on windows form application which is HTML base user interface. I need to know when the web page is loaded completely. I've tested many web browser events like DocumentCompleted, IsBusy, ReadyState but none of them responded what i expected.

Upvotes: 4

Views: 1542

Answers (2)

Alex F
Alex F

Reputation: 3539

Chamika Sandamal is correct - you should use DocumentComplete event BUT - check the 'sender' object. The very last 'complete' event is coming from 'browser' object itself and not from images, text, etc.. that fire it on loading. After all elements on page will fire DocumentComelete event the very last event will come from browser itself. If you could cast the 'sender' to browser object - here you go - it's browser loading complete event. Just notice that in case you have any 'frame' tags in HTML they will rise different DcoumentComplete events after browser Complete event. I think 'frame' considered as another HTML page so it will have itself 'complete' events..

Upvotes: 0

sngregory
sngregory

Reputation: 406

If you can use the jQuery library, then it's really simple.

$(document).ready() {
    //your page is fully loaded
};

Otherwise you'll have to have to rely on different methods based on the browser you're using. Since it's a windows form application, I'm assuming the rendering engine you're using is IE based. If that's then this might work for you:

if (document.attachEvent)
{
 document.attachEvent("onreadystatechange", function()
 {
      if (document.readyState === "complete")
      {
           document.detachEvent("onreadystatechange",
                arguments.callee);
           /* code to run on load */
      }
 });
}

You can find other browser dependent solutions here, if you're interested: http://dean.edwards.name/weblog/2006/06/again/

Upvotes: 3

Related Questions