2

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

noseratio
  • 59,932
  • 34
  • 208
  • 486
Karam Najjar
  • 527
  • 5
  • 21
  • It will be hard to get this right given the number of websites that makes additional server queries via Javascript AFTER the page is loaded. – Crono Feb 11 '14 at 18:07
  • You could unbind the event after hitting DocumentCompleted the first time, to ensure it's only fired once. Then rebind before you call webBrowser1.Navigate(url); – scheien Feb 11 '14 at 18:10
  • Perhaps, this can help http://stackoverflow.com/questions/635948/c-sharp-webbrowser-control-get-document-elements-after-ajax – Artyom Neustroev Feb 11 '14 at 20:28

1 Answers1

2

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.

Community
  • 1
  • 1
noseratio
  • 59,932
  • 34
  • 208
  • 486