HTML - How do I know when all frames are loaded?

前端 未结 12 1268
挽巷
挽巷 2020-12-10 04:31

I\'m using .NET WebBrowser control. How do I know when a web page is fully loaded?

I want to know when the browser is not fetching any more data. (The moment when IE

相关标签:
12条回答
  • 2020-12-10 05:21

    I'm not sure it'll work but try to add a JavaScript "onload" event on your frameset like that :

    function everythingIsLoaded() { alert("everything is loaded"); }
    var frameset = document.getElementById("idOfYourFrameset");
    if (frameset.addEventListener)
        frameset.addEventListener('load',everythingIsLoaded,false); 
    else
        frameset.attachEvent('onload',everythingIsLoaded); 
    
    0 讨论(0)
  • 2020-12-10 05:21

    Here's what finally worked for me:

           public bool WebPageLoaded
        {
            get
            {
                if (this.WebBrowser.ReadyState != System.Windows.Forms.WebBrowserReadyState.Complete)
                    return false;
    
                if (this.HtmlDomDocument == null)
                    return false;
    
                // iterate over all the Html elements. Find all frame elements and check their ready state
                foreach (IHTMLDOMNode node in this.HtmlDomDocument.all)
                {
                    IHTMLFrameBase2 frame = node as IHTMLFrameBase2;
                    if (frame != null)
                    {
                        if (!frame.readyState.Equals("complete", StringComparison.OrdinalIgnoreCase))
                            return false;
    
                    }
                }
    
                Debug.Print(this.Name + " - I think it's loaded");
                return true;
            }
        }
    

    On each document complete event I run over all the html element and check all frames available (I know it can be optimized). For each frame I check its ready state. It's pretty reliable but just like jeffamaphone said I have already seen sites that triggered some internal refreshes. But the above code satisfies my needs.

    Edit: every frame can contain frames within it so I think this code should be updated to recursively check the state of every frame.

    0 讨论(0)
  • 2020-12-10 05:22

    Here's how I solved the problem in my application:

    private void wbPost_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        if (e.Url != wbPost.Url)
            return;
        /* Document now loaded */
    }
    
    0 讨论(0)
  • 2020-12-10 05:22

    Have you tried WebBrowser.IsBusy property?

    0 讨论(0)
  • 2020-12-10 05:23

    I just use the webBrowser.StatusText method. When it says "Done" everything is loaded! Or am I missing something?

    0 讨论(0)
  • 2020-12-10 05:24

    Checking for IE.readyState = READYSTATE_COMPLETE should work, but if that's not proving reliable for you and you literally want to know "the moment when IE writes 'Done' in its status bar", then you can do a loop until IE.StatusText contains "Done".

    0 讨论(0)
提交回复
热议问题