title: using Silverlight 3's HtmlPage.Window.Navigate method to reuse an already open browser window
body: <p>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).</p>
  
  <p>Now, to achieve this, I simply use </p>
  
  <pre><code>HtmlPage.Window.Navigate(new Uri("http://www.bing.com"), "_blank");
  </code></pre>
  
  <p>which works fine.</p>
  
  <p>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:</p>
  
  <p><b>Option 1</b> - Use the overload of the Navigate method, like so:</p>
  
  <pre><code>HtmlPage.Window.Navigate(new Uri("http://www.bing.com"), "foo");
  </code></pre>
  
  <p>I was assuming that the window would be reused when the same target parameter value (foo) would be used in subsequent calls.<br>
  This does not work.  I get a new window every time.</p>
  
  <p><b>Option 2</b> - Use the PopupWindow method on the HtmlPage</p>
  
  <pre><code>HtmlPage.PopupWindow(new Uri("http://www.bing.com"), "blah", new HtmlPopupWindowOptions());
  </code></pre>
  
  <p>This does not work.  I get a new window every time.</p>
  
  <p><b>Option 3</b> - Get a handle to the opened window and reuse that in subsequent calls</p>
  
  <pre><code>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");
  }
  </code></pre>
  
  <p>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, <a href="http://msdn.microsoft.com/en-us/library/system.windows.browser.htmlpage.popupwindow%28VS.95%29.aspx" rel="nofollow noreferrer">as they should</a>.</p>
  
  <p><b>Option 4</b> - Use Eval method to execute some custom javascript</p>
  
  <pre><code>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);
  }
  </code></pre>
  
  <p>This does not work.  I get a new window every time.</p>
  
  <p><b>option 5</b> - Use CreateInstance to run some custom javascript on the page</p>
  
  <pre><code>private void navigationButton_Click(object sender, RoutedEventArgs e)
  {
      HtmlPage.Window.CreateInstance("thisIsPlainHell");
  }
  </code></pre>
  
  <p>and in my aspx I have</p>
  
  <pre><code>function thisIsPlainHell() {
      var popup = window.open('http://www.bing.com', 'blah');
  }
  </code></pre>
  
  <p>This doesn't work.  I get a new window every time.</p>
  
  <p>Am I doing something wrong?  I'm definitely no javascript expert, so I'm hoping it's something obvious I'm missing here.</p>
  
  <p>Cheers, 
  Phil</p>