Give an warning to user before leaving the page | Web Programming Tutorial

Give an warning to user before leaving the page

Published Date: January 11, 2013 |
Published Time: 9:23 pm |

Sometime we need to give user an warning message when user leave the page without saving his/her input/changes.It may happens for various reason, sometimes user may forget to save and leave the page, or sometimes accidently click other link and then leave the page.So we can give user a warning before leave the page, and he can stay in the page if want.

We will javascript beforeunload event .This is supported by almost all browsers, expect of Opera which is known to adhere the W3C standards extremely strictly.

write the code in that page where you want to give the warning message.

it will display an warning message when you attempt to leave the page.Different browser will display different type of window.

In Mozilla Firefox , the window will look lik :

jquery

In your specific case you need to turn it off (just set to null) whenever a navigational link is clicked or an internal form is submitted. You can do that by listening on the click event of the desired links and the submit event of the desired forms. jQuery may be of great help here:

So now the warning will not be displayed when you submit form or any link for which you don’t want to display the warning.

Leave a Reply

Your email address will not be published. Required fields are marked *