Tuesday, August 11, 2009

JavaScript location.href instead of Silverlight HtmlPage.Window.Navigate

MSDN documentation for Silverlight HtmlPage.Window.Navigate method is pretty basic, here is the link. Summary for the method is: "Opens the specified page in the specified browser instance."
But, behind the scene, if you look at the code in Reflector, HtmlPage.Window.Navigate method invokes JavaScript "open" method. That's all right if you want to call popups, but what if you want to navigate to html page in the same window without calling the popup?
I've encountered following problems with using HtmlPage.Window.Navigate:
  • It looks like there are some problems with the browser's cache when Navigate method is used, as described by Kevin Hazzard here. Furthermore, in case you use Asp.Net postbacks, the popup page can lead to corrupted viewstate in some scenarios.
  • HtmlPage.Window.Navigate method's just stopped working in recent builds of Google Chrome browser, probably because of some internal pop-up blocker code.
Therefore, I've decided to use JavaScript location.href instead of HtmlPage.Window.Navigate when target navigation page is set to "_self". Code snippet which looks like a possible solution is published on Rick Strahl's great CodePaste tool here.

I would like to hear your thoughts and experiences on this.


  1. Providing code samples as images is really great.

  2. Thanks! This perfectly explained and fixed the problem I was having with the viewstate after a Silverlight navigation.