tags:

views:

804

answers:

6

How can i detected WebBrowser control complete loading? i tried to use Navigate and DocumentCompleted events but both of them raised a few times during document loading!

A: 

You could use JQuery

 $(document).ready(function() {
   // put all your jQuery goodness in here.
 });

http://docs.jquery.com/Tutorials:Introducing_$(document).ready()

Robert Williams
Thanks but i am using c# :)
Neir0
This might not be exactly what Neir0 is after. From the same doc:"Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded. " So images etc aren't taken into account.
kibibu
I assumed you were building an ASP.NET web application and could add the JQuery library into your solution. If this is an incorrect assumption, please provide more information in your question.
Robert Williams
@Robert - `WebBrowser` is a forms control, and it is basically a wrapper of MSHTML. With that in mind, his question makes perfect sense.
Kyle Rozendo
+3  A: 

I think that the DocumentCompleted event will get fired for all child documents that are loaded as well (like JS and CSS, for example). You could look at the WebBrowserDocumentCompletedEventArgs in DocumentCompleted and check the Url property and compare that to the Url of the main page.

Paul Kearney - pk
That's what I figured out from fighting this problem myself. I was getting a bunch of extra completions on things like ad banners. I discard any event that doesn't match the URL I'm after and it works.
Loren Pechtel
+1  A: 

It doesn't seem to trigger DocumentCompleted/Navigated events for external Javascript or CSS files, but it will for iframes. As PK says, compare the WebBrowserDocumentCompletedEventArgs.Url property (I don't have the karma to make a comment yet).

Jivlain
A: 

If you're using WPF there is a LoadCompleted event.

If it's Windows.Forms, the DocumentCompleted event should be the correct one. If the page you're loading has frames, your WebBrowser control will fire the DocumentCompleted event for each frame (see here for more details). I would suggest checking the IsBusy property each time the event is fired and if it is false then your page is fully done loading.

alimbada
A: 

I did the following:

void BrowserDocumentCompleted(object sender,
        WebBrowserDocumentCompletedEventArgs e)
{
  if (e.Url.AbsolutePath != (sender as WebBrowser).Url.AbsolutePath)
    return; 

  //The page is finished loading 
}

The last page loaded tends to be the one navigated to, so this should work.

From here.

Kyle Rozendo
+1  A: 

Note the url in DocumentCompleted can be different than navigating url due to server transfer or url normalization (e.g. you navigate to www.microsoft.com and got http://www.microsoft.com in documentcomplete)

In pages with no frames, this event fires one time after loading is complete. In pages with multiple frames, this event fires for each navigating frame (note navigation is supported inside a frame, for instance clicking a link in a frame could navigate the frame to another page). The highest level frame, which may or may not be the top level browser, fires the final DocumentComplete event.

In native code you would compare the sender of the DocumentComplete event to determine if the event is the final event in the navigation or not. However in Windows Forms the sender parameter is not wrapped by WebBrowserDocumentCompletedEventArgs. You can either sink the native event to get the parameter's value, or check the readystate property of the browser or frame documents in the DocumentCompleted event handler to see if all frames are in the ready state.

There is a prolblem with the readystate method as if a download manager is present and the navigation is to a downloadable file, the navigation could be cancelled by the download manager and the readystate won't become complete.

Sheng Jiang 蒋晟