2

My software handles navigation to target=_new through this function

Private Sub Web1_NewWindow(...) Handles Web1.NewWindow
    Web1.Navigate(Web1.StatusText)
    e.Cancel = True
End Sub

Which allows me to open any new windows inside the same webbrowser control. However, when navigating to a "javascript:" link that creates a new popup, I get the following message because it is trying to navigate to that page:

The requested resource is in use. (Exception from HRESULT: 0x800700AA)

How can I make it open a javascript popup in the webbrowser control?

The link looks like this:

javascript:Dpy.ITQPopup('100',255,'2932 NTYwNDUwMTA0MDYzMDM);3094 V0FZ','-357933312',0,0)
4
  • That is not possible. The Automation interface can only deal with popups, not Javascript alert() chatter. Best to find a way to avoid that horrible error message, I'd say, putting it a friendly window isn't going to make the user any happier. Commented Sep 15, 2013 at 21:19
  • hey Hans, I'm not talking about javascript:alert(), i'm talking about a new window created as a result of a script by clicking the link "javascript:function(xyz)". Commented Sep 15, 2013 at 21:30
  • Same problem. Works just lovely in my favorite browser's "open link in new tab" context menu entry as well. Not. Commented Sep 15, 2013 at 21:33
  • What if I implemented tabs? Commented Sep 15, 2013 at 21:55

1 Answer 1

3

Depending on what exactly is inside the javascript: link, it may or may not work. For example:

<body>

<!-- These work -->

<a href="http://www.example.com" target="_blank">Test 0</a><br>
<a href="javascript:navigate('http://www.example.com')" target="_blank">Test 1</a><br>
<a href="javascript:'<b>hello</b> from new page!'" target="_blank">Test 2</a><br>
<a href="javascript:''" target="_blank">Test 3</a><br>
<a href="javascript:TestFunc()" target="_blank">Test 4</a><br>

<!-- This does not work -->

<a href="javascript:open('http://www.example.com')" target="_blank">Test 5</a><br>


<script>
function TestFunc()
{
    return "<b>TestFunc</b> result.";
}
</script>

</body>

Here's the code that handles NewWindow event. This code is in C#, unfortunately I don't know VB.NET good enough to properly translate it.

private void Form1_Load(object sender, EventArgs e)
{
    // this code depends on SHDocVw.dll COM interop assembly,
    // generate SHDocVw.dll: "tlbimp.exe ieframe.dll",
    // and add as a reference to the project

    var activex = (SHDocVw.WebBrowser_V1)this.webBrowser.ActiveXInstance;

    activex.NewWindow += delegate(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
    {
        Processed = true;
        object flags = Flags;
        object targetFrame = Type.Missing;
        object postData = PostData != null ? PostData : Type.Missing;
        object headers = !String.IsNullOrEmpty(Headers)? Headers.ToString() : Type.Missing;
        activex.Navigate(URL, ref flags, ref targetFrame, ref postData, ref headers);
    };

    this.webBrowser.Navigate("http://localhost:81/new-window-test.html");
}

Now, if you really need to make Test 5 work, it's still possible. The problem with it is there are in fact two navigations, the second one is nested, which might be the reason for error 0x800700AA. The trick is to return from NewWindow event first, and then do the navigation:

private void Form1_Load(object sender, EventArgs e)
{
    // this code depends on SHDocVw.dll COM interop assembly,
    // generate SHDocVw.dll: "tlbimp.exe ieframe.dll",
    // and add as a reference to the project

    var activex = (SHDocVw.WebBrowser_V1)this.webBrowser.ActiveXInstance;

    activex.NewWindow += delegate(string URL, int Flags, string TargetFrameName, ref object PostData, string Headers, ref bool Processed)
    {
        Processed = true;
        object flags = Flags;
        object targetFrame = Type.Missing;
        object postData = PostData != null ? PostData : Type.Missing;
        object headers = !String.IsNullOrEmpty(Headers) ? Headers.ToString() : Type.Missing;
        SynchronizationContext.Current.Post(delegate
        {
            activex.Navigate(URL, ref flags, ref targetFrame, ref postData, ref headers);
        }, null);
    };

    this.webBrowser.Navigate("http://localhost:81/new-window-test.html");
}

Tested with IE10.

[UPDATE]

Try this (verified):

Public Class Form1
    Dim ActiveX As SHDocVw.WebBrowser_V1

    Private Sub NavigateOnNewWindow(NewWindowUrl As Object)
        Me.ActiveX.Navigate(NewWindowUrl.ToString())
    End Sub

    Private Sub NewWindow(URL As String,
                          Flags As Integer,
                          TargetFrameName As String,
                          ByRef PostData As Object,
                          Headers As String,
                          ByRef Processed As Boolean)
        Processed = True
        System.Threading.SynchronizationContext.Current.Post(AddressOf NavigateOnNewWindow, URL)
    End Sub

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' Init DocumentText, otherwise Me.WebBrowser1.ActiveXInstance is null, this is different from C#
        Me.WebBrowser1.DocumentText = String.Empty
        Me.ActiveX = Me.WebBrowser1.ActiveXInstance
        AddHandler Me.ActiveX.NewWindow, AddressOf NewWindow
        Me.WebBrowser1.Navigate("http://localhost:81/new-window-test.html")
    End Sub
End Class
Sign up to request clarification or add additional context in comments.

9 Comments

I added the link to the original post, maybe you can help me identify whether it's test 5 or not.
Does ITQPopup(...) return anything? Does it call window.open inside?
I just traced the functions and yes, window.open is called!
Then yes, it's the case #5 in my test, because of window.open. I updated the answer with some VB.NET code, give it a try.
You, my friend, are a boss, thank you very much - and to whoever else is implementing this, MAKE SURE!!!! you do not have a NewWindow event for the WebBrowser control as it takes precedence over the ActiveX.NewWindow event.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.