Programe to Confirm Leaving of page using javaScript

for example if you have a scenario that to display confirm leaving when ever user is leaving the page by closing browser or by clicking on any part of other page lie, clicking on buttons or Links

for that we need to write one simple javascript function

var isChange =1;

function ConfirmLeaving()


                 return “All the entered information will be lost. Do you wish to continue?”;


in Body tag call a function on beforeunload  as onbeforeunload=”return confirmleaving();”


so here when ever a user try’s to navigate from the page or perform operations for postback this event is fired

suppose if you want any clicking on some button named “Button1” should not get this message you can simply place in onclick() function to set isChange=1 so that the above written condition will satisfy and which wont show the popup

you can place isChange for div for example

if i have a div which have so many buttons and clicking on those many buttons should not give this confirmation message so for this i need to set my flag isChange when ever user mouseout or mousein from the div like this

<div  onmouseover=”isChange = 1″ onmouseout=”isChange = 0″ >





when ever the user try’s to click outside the div he will get confirmation message and when user click’s inside div confirmation is not fired this is achived using a flag 🙂


Have a Happy coding


Santosh Kakani


About kakani santosh kumar

Working as Software Engineer in Prithvi Information Solutions having 3 years of Experience in Dotnet Framework ranging from 1.1 to 3.5
This entry was posted in Uncategorized and tagged , . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s