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)
{
    System.Windows.Browser.HtmlPage.Window.Invoke("close");
}

 

 

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.

 

SNAGHTMLfcd46a3


12 comments

  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

    ReplyDelete
  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.

    Regards,
    Kunal

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

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

    ReplyDelete
  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.

    ReplyDelete
  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?

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

    function CloseWindow()
    {
    window.close();
    }

    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)
    {
    System.Windows.Browser.HtmlPage.Window.Invoke("CloseWindow");
    }

    Maciej

    ReplyDelete
  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?

    ReplyDelete
  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 ;)

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

    ReplyDelete
  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..

    ReplyDelete


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