How to Close Browser Window from Silverlight Application?

All most all secured sites like bank websites asks you to close your browser window after you signed out from the web application. This is due to high security which actually removes all session details from the browser cache.


If you want to develop the same behavior in your Silverlight application, how will you do this? This post covers the code to implement the functionality of the same. Read to know more.



If you are developing a secured site and want to close the browser window just after the user logout from the application, this small tip will help you. This is not a difficult job to implement. Just a single line of code will do the trick for you.


Use "System.Windows.Browser.HtmlPage.Window.Invoke()" method to call the Close() method of the browser window as shown in the below code snippet:

private void OnCloseClick(object sender, RoutedEventArgs e)



The above code when called will close the browser window where your Silverlight application is hosted. If it is a tab, it will close the Window tab instead. If you are using it inside the Internet Explorer, it will confirm you whether you really want to close the browser. If you press "No", it will remain in that page and clicking "Yes" will close the browser tab/window.



If you have come this far, it means that you liked what you are reading. Why not reach little more and connect with me directly on Twitter, Facebook, Google+ and LinkedIn. I would love to hear your thoughts and opinions on my articles directly.

Also, don't forget to share your views and/or feedback in the comment section below.


  1. thanks for this Tip
    but when i use it it FF4 it close the Browser and all tabs
    i want to show popup window like in IE

  2. Hi,

    It should not close all the tabs. It should only close the current tab. This actually calls the JavaScript method close() which is not part of the Silverlight.

    Regarding your second query, the popup message is handled by the browser itself. For IE, it asks the user for confirmation but for other browsers like Chrome, FF it doesn't ask.

    If you want to show a confirmation, you can use the Confirm() method but before that, you have to check which browser it is. Make sure that, if any API changes for browser, this method may behave incorrectly.


  3. Thanks for the info, but it does not work in Chrome.

  4. I think, you asked this question in Silverlight.Net forum. It works for all browsers. I tested in IE, FF and Chrome.

  5. I tried it and it works like a charm in IE.

    My understanding is that this Invoke command can also be used to call Javascript on the page. It might be easier to detect the browser type etc... from there, if someone wants to extend the functionality as you suggested.

  6. Yes, you will be able to call the JavaScript. And, if you want to retrieve the browser information, this post might help you: How can you detect Browser Information in Silverlight?

  7. To avoid that the IE dialog poping up, you could put some javascript code into your hosting page:

    function CloseWindow()

    and call that method from Silverlight (just copied from above and changed the invoked method name, since I rather use commands instead of click events):

    private void OnCloseClick(object sender, RoutedEventArgs e)


  8. Hi Maciej,
    Thanks for the code snippet shared here. Hope this will be helpful to someone.
    BTW, I have one query. Will it support all browsers or is it specific for IE only?

  9. Hi, Kunal,

    I have only Firefox 6 and IE 9 on my machine (at client side) and that trick seems to work in both browsers. Ofcourse, as you know, you must allow JavaScript for the particular hosting page.

    Thanks for the article. I am a reading your blogs and I must say some topics come right at hand, as I need them ;)

  10. Hi Maciej,
    Thanks for the info. I will try it out in my PC.
    Again, thanks for the feedback and support.

  11. Hi Kunal,

    I just want to know that if I want to close my specific page in silverlight application than this code is not working as per what I want can you help in my problem. I am a beginner in silverlight so need help over here..

  12. Any thoughts as to why the Invoke("close") throws InvalidOperationException?
    Scenario: Silverlight app creates browser window with
    win = HtmlPage.Window.Navigate(new Uri("https://www.abc.com", ...)
    the win.Invoke("close") throws the exception.
    Would the cross-domain nature of the URL have something to do with it?


© 2008-2018 Kunal-Chowdhury.com | Designed by Kunal Chowdhury
Back to top