none
Webbrowser / htmlwindow: How to identify window created by javascripr window.open

    Question

  • VS2008/Visual Basic: After navigating a webrowser to a URL containing javascript links which create html popups via onclick > window.open, what is the best way to programatically identify the resulting popup windows and access their html contents with hmlwindow?  Thanks - BGood

    Tuesday, April 22, 2008 3:46 AM

Answers

  • Hi, BGood

    Thanks for you post.

    You can cast the WebBrowser.ActiveXInstance to a SHDocVx.WebBrowser object, then handle its NewWindow3 event to retrieve the URL of the popup window, with the URL you can access the html content of the popup window by using HttpWebRequest class.

    Code Snippet

    private void Form1_Load(object sender, EventArgs e)

            {

                this.webBrowser1.Navigate(@"c:\test2\a.htm");

                this.webBrowser1.NewWindow += new CancelEventHandler(webBrowser1_NewWindow);

     

                SHDocVw.WebBrowser axBrowser =

                    (SHDocVw.WebBrowser)this.webBrowser1.ActiveXInstance;

                axBrowser.NewWindow3 +=

                    new SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(axBrowser_NewWindow3);

            }

     

            void webBrowser1_NewWindow(object sender, CancelEventArgs e)

            {

                e.Cancel = true; //cancel the navigating

            }

     

            void axBrowser_NewWindow3(ref object ppDisp,

                ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl)

            {

                // access the web page with the URL bstrUrl

            }

     

     



    Hope this helps.
    If you have any more questions, please feel free to let me know.Thanks in advance for any feedback. I am standing by to be of assistance.

    Best Regards
    Zhi-xin Ye

    Monday, April 28, 2008 7:43 AM

All replies

  • Moderator, I have not received any responses to this question.  If there is a better forum for this posting, please feel free to move it as I am still in need of help.  Thanks, -BGood

    Saturday, April 26, 2008 3:26 PM
  • Hi, BGood

    Thanks for you post.

    You can cast the WebBrowser.ActiveXInstance to a SHDocVx.WebBrowser object, then handle its NewWindow3 event to retrieve the URL of the popup window, with the URL you can access the html content of the popup window by using HttpWebRequest class.

    Code Snippet

    private void Form1_Load(object sender, EventArgs e)

            {

                this.webBrowser1.Navigate(@"c:\test2\a.htm");

                this.webBrowser1.NewWindow += new CancelEventHandler(webBrowser1_NewWindow);

     

                SHDocVw.WebBrowser axBrowser =

                    (SHDocVw.WebBrowser)this.webBrowser1.ActiveXInstance;

                axBrowser.NewWindow3 +=

                    new SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(axBrowser_NewWindow3);

            }

     

            void webBrowser1_NewWindow(object sender, CancelEventArgs e)

            {

                e.Cancel = true; //cancel the navigating

            }

     

            void axBrowser_NewWindow3(ref object ppDisp,

                ref bool Cancel, uint dwFlags, string bstrUrlContext, string bstrUrl)

            {

                // access the web page with the URL bstrUrl

            }

     

     



    Hope this helps.
    If you have any more questions, please feel free to let me know.Thanks in advance for any feedback. I am standing by to be of assistance.

    Best Regards
    Zhi-xin Ye

    Monday, April 28, 2008 7:43 AM
  • Hi Zhi-xin Ye,

     

    Thanks for the reply, but since your suggested approach is in C# rather than VB, I'm having a little trouble implementing your suggestion since I need to translate into VB. 

     

    Your answer also implies the use of SDDocVx.WebBrowser and I WAS using WebBrowser 2.0 in System.windows.forms which doesn't seem like it implements a Navigate3 or Navigate2 interface.


    So if I understand correctly, it looks like I need to provide an alternative event handler to override javascript's window.open.  Can this be done in VB rather than C#?

     

    Thanks, -BGood

    • Proposed as answer by MosIdiot Monday, May 3, 2010 6:21 AM
    Saturday, May 3, 2008 6:42 PM
  • What if this is done in a plane old Standalone app/ window, not forms.

    Do you have an example for that?

     

    Thank you in advanced.

    Wednesday, February 2, 2011 6:01 PM