|
|
|
|
How to add confirmation when leaving a web page
Very useful Javascript feature that can prevent a user from losing the data they are entering in an entry field or text editor in case of unintentional leaving of a page.
|
|
 |
|
|
|
|
|
 |
|
 |
|
 |
 |
1.
|
|
|
Create a new or existing HTML web page.
You can skip to the step 4 if you are only after the Javascript code.
|
|
|
2.
|
|
|
Add some text or an image.
It's not really necessary.
Click Code to enter the Code view.
|
|
|
3.
|
|
|
Click somewhere between the <head> and </head> tags.
Meaning we will add the script in the head area.
|
|
|
4.
|
|
|
Now enter or copy/paste the following code:
<script> function closeEditorWarning(){ return '' }
window.onbeforeunload = closeEditorWarning </script>
Between the two ' ' characters next to return, you can add you own personalized message that users will see when leaving a page, our you can leave the default version.
|
|
|
5.
|
|
|
Save the web page and press F12 to test the script.
|
|
|
6.
|
|
|
So when the user leaves the page they will need to confirm that they are really want to leave it.
As already mentioned, this can come in really handy on the sites that use entry forms and text editors. Sometimes it can that you are in the middle of typing a really long text and you unintentional click some link or the back button and your text is gone. Otherwise it's not recommended to use this Javascript feature as it can be quite frustrating for the user.
|
|
|
 |
 |
 |
|
 |
|
|