Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

How to Close the Browser Window from a Silverlight Application

0.00/5 (No votes)
9 Aug 2011 1  
If you are developing a secured site and want to close the browser window just after the user logs out from the application, this small tip will help you.

Almost all secured sites like bank websites ask you to close your browser window after you sign out from a web application. This is a security measure 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 this functionalityRead to know more.

If you are developing a secured site and want to close the browser window just after the user logs out 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 the "System.Windows.Browser.HtmlPage.Window.Invoke()" method to call the Close() method of the browser window, as shown in the below code snippet:

C#
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 Internet Explorer, it will ask 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_3_.png

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here