Alert/Prevent Users From closing the browser

Here's a simple script that works on both IE and Mozilla and displays a confirmation box before closing the browser. You can use this script to detect if an activity is on and alert users about the same, before they close the browser.

<html xmlns="">

<head runat="server">


    <script type="text/javascript"> 

        window.onbeforeunload = function(e) {

            e = e || window.event;

            e.returnValue = "Your message here";





    <form id="form1" runat="server">







You can use the same script on ASP.NET pages too

Will you give this article a +1 ? Thanks in advance

About The Author

Suprotim Agarwal
Suprotim Agarwal, ASP.NET Architecture MVP (Microsoft Most Valuable Professional) works as an Architect Consultant and provides consultancy on how to design and develop Web applications.

Suprotim is also the founder and primary contributor to DevCurry, DotNetCurry and SQLServerCurry. He is the Editor of a Developer Magazine called DNC Magazine. He has also written two EBooks 51 Recipes using jQuery with ASP.NET Controls. and The Absolutely Awesome jQuery CookBook

Follow him on twitter @suprotimagarwal

1 comment:

Sandeep Aparajit said...

The pop-up is shown even if you navigate to other page of your own website. I wanted a solution wherein the user will be altered ONLY if he is closing the Browser window. Can you pls provide any pointers?