using Silverlight 3's HtmlPage.Window.Navigate method to reuse an already open browser window

Posted by Phil on Stack Overflow See other posts from Stack Overflow or by Phil
Published on 2010-03-24T09:19:53Z Indexed on 2010/03/24 9:23 UTC
Read the original article Hit count: 1217

Hi,

I want to use an external browser window to implement a preview functionality in a silverlight application. There is a list of items and whenever the user clicks one of these items, it's opened in a separate browser window (the content is a pdf document, which is why it is handled ouside of the SL app).

Now, to achieve this, I simply use

HtmlPage.Window.Navigate(new Uri("http://www.bing.com"));

which works fine.

Now my client doesn't like the fact that every click opens up a new browser window. He would like to see the browser window reused every time an item is clicked. So I went out and tried implementing this:

Option 1 - Use the overload of the Navigate method, like so:

HtmlPage.Window.Navigate(new Uri("http://www.bing.com"), "foo");

I was assuming that the window would be reused when the same target parameter value (foo) would be used in subsequent calls.
This does not work. I get a new window every time.

Option 2 - Use the PopupWindow method on the HtmlPage

HtmlPage.PopupWindow(new Uri("http://www.bing.com"), "blah", new HtmlPopupWindowOptions());

This does not work. I get a new window every time.

Option 3 - Get a handle to the opened window and reuse that in subsequent calls

private HtmlWindow window;
private void navigationButton_Click(object sender, RoutedEventArgs e)
{
    if (window == null)
        window = HtmlPage.Window.Navigate(new Uri("http://www.bing.com"), "blah");
    else
        window.Navigate(new Uri("http://www.bing.com"), "blah");

    if (window == null)
        MessageBox.Show("it's null");
}

This does not work. I tried the same for the PopupWindow() method and the window is null every time, so a new window is opened on every click. I have checked both the EnableHtmlAccess and the IsPopupWindowAllowed properties, and they return true, as they should.

Option 4 - Use Eval method to execute some custom javascript

private const string javascript = @"var popup = window.open('', 'blah') ; 
                                    if(popup.location != 'http://www.bing.com' ){
                                        popup.location = 'http://www.bing.com';
                                    }
                                    popup.focus();";

private void navigationButton_Click(object sender, RoutedEventArgs e)
{
    HtmlPage.Window.Eval(javascript);
}

This does not work. I get a new window every time.

option 5 - Use CreateInstance to run some custom javascript on the page

private void navigationButton_Click(object sender, RoutedEventArgs e)
{
    HtmlPage.Window.CreateInstance("thisIsPlainHell");
}

and in my aspx I have

function thisIsPlainHell() {
    var popup = window.open('http://www.bing.com', 'blah');
    popup.focus();
}

Guess what? This does work. The only thing is that the window behaves a little strange and I'm not sure why:

  • I'm behind a proxy and in all other scenarios I'm being prompted for my password. In this case however I am not (and am thus not able to reach the external site -> bing in this case). This is not really a huge issue atm, but I just don't understand what's goign on here.
  • Whenever I type another url in the address bar of the popup window (eg www.google.com) and press enter, it opens up another window and prompts me for my proxy password.

As a temporary solution option 5 could do, but I don't like the fact that Silverlight is not able to manage this. One of the main reasons my client has opted for Silverlight is to be protected against all the browser specific hacking that comes with javascript.

Am I doing something wrong? I'm definitely no javascript expert, so I'm hoping it's something obvious I'm missing here.

Cheers, Phil

© Stack Overflow or respective owner

Related posts about Silverlight

Related posts about silverlight-3.0