Detecting when the webpage is fully loaded with we

2019-05-21 01:05发布

问题:

I'm using a WebBrowser control , there is an event which is called DocumentCompleted , this event is fired for each frame in the web page and also for all the child documents (e.g. JS and CSS) that are loaded.

my question is how to detect the last entry of this event, I mean how to detect when the page is fully loaded , and this event is not firing again .

I looked for this a lot , I found a solution like this :

void webBrowser1_DocumentCompleted(object sender,
        WebBrowserDocumentCompletedEventArgs e)
{
    if (e.Url.Equals(webBrowser1.Url)) {
        // Here the page is fully loaded        
    }
}

but this is not working with all the urls !

Anybody has an idea how to achieve that ?

thanx in advance

回答1:

The DocumentComplete event may be fired multiple times for a document with frames. At the same time, for a particular document, the HTML DOM window.onload event will be fired only once. Here is how to use this fact to handle the completion of the top page loading.

OTOH, some pages are quite complex and use continuous AJAX updates. Strictly speaking, it may not always be possible to determine when a page like that has finished its rendering, with 100% probability. However, you can get close, here is how.