Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Windows Forms / WinForm Controls / May 2008

Tip: Looking for answers? Try searching our database.

How to pass the IE windows handle out of the WebBrowser?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Sean - 30 Apr 2008 22:16 GMT
I am using a WebBrowser control to access a third-party website in my
application. I need to intercept the pop up IE window event on the website
and display page in another WebBrowser control so that I can parse the DOM
object in the pop up page.

    Private Sub webWebBrowser_NewWindowExtended(ByVal sender As Object, ByVal e
As iQControls.WebBrowserNewWindowExtendedEventArgs) Handles
webWebBrowser.NewWindowExtended
        e.Cancel = True

        string url = e.Url

        ... ' display the url in another webbrowser so that we can parse
the page

    End Sub

It worked fine. But we suffer changes applied on the website recently. Here
is the JavaScript to open the pop up window I found on the latest website.

url = "blank.html";
w = open(url, windowName,
'width=690,height=600,screenX=0,screenY=0,scrollbars=yes,menubar=yes');
...
w.document.write(s);  ' write the page content
w.document.close();
w.focus();

The w returned by open() is always null because we set the e.Cancel to true
in our WebBrowser. And if we set the e.Cancel to false, we get a pop-up IE
window with desired content and a webbrowser window with blank content. But
in this way we can't parse DOM object.

It may be a solution if we can pass the handle of IE instance inside the
WebBrowser control back to JavaScript. I am searching for that. I'd like to
know if there anybody knows how to do it?

Any help is appreciated.
Sheng Jiang[MVP] - 30 Apr 2008 23:36 GMT
You need to handle the NewWindow2 event and pass your new window's
webbrowser's interface back to the event argument (NewWindow2)
.Net does not provide direct access of this event, however, there are many
open source code on the internet that wrap this event. One example is at
www.codeproject.com/KB/miscctrl/csEXWB.aspx

Signature

Sheng Jiang
Microsoft MVP in VC++

> I am using a WebBrowser control to access a third-party website in my
> application. I need to intercept the pop up IE window event on the website
[quoted text clipped - 34 lines]
>
> Any help is appreciated.
Sean - 05 May 2008 16:31 GMT
Hi Sheng,

Thanks for your reply.

I tried the NewWindow2 with csExWB but it does not work.

Here is the index.html I accessed.

<script language="JScript">
function rewriteServiceAgreement(){
 page="RegenerateServiceAgreement";

 var windowName = "SA_";

 var w = null;

 var blankname= "blank.html";
 var retry=0;
 while (w == null && retry<100) {
   if(retry > 0) {
     alert('Regenerating service agreement.');
   }
   alert(retry);
   w = open(blankname, windowName,
'width=890,height=600,screenX=0,screenY=0,scrollbars=yes,menubar=yes');
   retry++;
   alert(retry);
 }

 if(w == null) {
   alert('IE BUG: the dwindow is still null.');
 }else if(w.document == null){
   alert('IE BUG: the document is null.');
 }

 var s = "<body><html>test</html></body>";

 w.document.close();
 w.document.open();
 w.document.write(s);
 w.document.close();
 w.focus();
}
</script>
<body onclick="rewriteServiceAgreement();">
Click this page and window.open() is called.
</body>

Here is my handle code for event NewWindow2.

    string url;
       private void cEXWB1_NewWindow2(object sender,
csExWB.NewWindow2EventArgs e)
       {
            Form1 form = new Form1();
            form.navigate(url);         // We get the blank page.
            e.browser = form.Browser;  // form.Browser is a default .NET WebBrowser
control.
            form.Show();
       }

       private void cEXWB1_NewWindow3(object sender,
csExWB.NewWindow3EventArgs e)
       {
            url = e.url;
       }

But I never see the second alert(retry). It looks like the javascript is
hung up on the “open” statement once the event handle code has been executed.
And the javascript works if we comment e.browser = form.Browser.

Do you know what reason is and how to resolve it?

Any help is appreciated.

Sean

> You need to handle the NewWindow2 event and pass your new window's
> webbrowser's interface back to the event argument (NewWindow2)
[quoted text clipped - 45 lines]
> >
> > Any help is appreciated.
Sheng Jiang[MVP] - 05 May 2008 20:13 GMT
You are pass a reference of a .Net wrapper object, while the event expect an
reference to an WebBrowser ActiveX object.

Signature

Sheng Jiang
Microsoft MVP in VC++

> Hi Sheng,
>
[quoted text clipped - 65 lines]
> But I never see the second alert(retry). It looks like the javascript is
> hung up on the "open" statement once the event handle code has been
executed.
> And the javascript works if we comment e.browser = form.Browser.
>
[quoted text clipped - 34 lines]
> > > url = "blank.html";
> > > w = open(url, windowName,

'width=690,height=600,screenX=0,screenY=0,scrollbars=yes,menubar=yes');
> > > ...
> > > w.document.write(s);  ' write the page content
[quoted text clipped - 14 lines]
> > >
> > > Any help is appreciated.
Sean - 06 May 2008 23:16 GMT
Hi Sheng,

Thanks for your reply. I changed the Browser type from default WebBrowser to
AxSHDocVw.AxWebBrowser. But the javascript is still hung up.
     
       string url;
       private void cEXWB1_NewWindow2(object sender,
csExWB.NewWindow2EventArgs e)
       {
    Form1 form = new Form1();
    form.navigate(url);

    e.browser = form.Browser; // Browser is AxSHDocVw.AxWebBrowser type.

    form.Show();
       }

Here is the class Form1.
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public void navigate(string url)
    {
        this.axWebBrowser1.Navigate(url);
    }

    public AxSHDocVw.AxWebBrowser Browser
    {
        get { return axWebBrowser1; }
        set { axWebBrowser1 = value; }
    }
}

Do you have any suggestion? Any help is appreciated.

> You are pass a reference of a .Net wrapper object, while the event expect an
> reference to an WebBrowser ActiveX object.
[quoted text clipped - 137 lines]
> > > >
> > > > Any help is appreciated.
Sheng Jiang[MVP] - 09 May 2008 16:14 GMT
If you can get the ActiveX's IWebBrowser2 interface, return its Application
property to NewWindow2.

Signature

Sheng Jiang
Microsoft MVP in VC++

> Hi Sheng,
>
[quoted text clipped - 62 lines]
> > >     alert(retry);
> > >     w = open(blankname, windowName,

'width=890,height=600,screenX=0,screenY=0,scrollbars=yes,menubar=yes');
> > >     retry++;
> > >     alert(retry);
[quoted text clipped - 109 lines]
> > > > >
> > > > > Any help is appreciated.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.