Ask user before closing the browser - IE and Mozilla

If you would like to ask the user for confirmation before closing the browser , then here's how to do so. You can use this script on an ASP.NET page or a simple HTML page. The code has been tested on IE 7 and Firefox 3+


<head runat="server">


<title></title>


<script type="text/javascript">


window.onbeforeunload = function() {


if (window.event)


window.event.returnValue = 'Thank you for visiting us!';


else


return 'Thank you for visiting us!';


}


</script>


</head>



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

3 comments:

Gautam said...

Hi Dear,

Have You test your written code to ask before close the browser.

your code is executing on every click on button or page load.

Please i would suggest you ,Please test your written code before publish on internet.

Suprotim Agarwal said...

Gautam: The code 'has' been tested :) This code has been tested on IE7, Firefox 3 and has been added to the post.

preeti said...

Have You test your written code to ask before close the browser.

your code is executing on every click on button or page load.