Dreevoo.com | Online Learning and Knowledge Sharing
 
Home | Programs | Adobe Dreamweaver | Settings | How to add confirmation when leaving a web page
Guest
Click to view your profile
Topics
Programs
Languages
Recipes
Home
Shortcuts
 
 

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.

 
  Author: NikMan | Version: CS5 | 7th February 2013 |  
 
 
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.

 
 
 
   
  Please login to post a comment
  Click to open user profile  
Humba, 11th Feb 2013, 1:29 AM
Really usable but I get this prompt also when submitting content? Is there any way to go around it
 
 
  Click to open user profile  
jackreyan, 21st Dec 2023, 6:43 AM
We run a community that promotes massage and leaves reviews, so why don't you come in and take a look?http://www.yangtao.com/
 
   
 
 
online learning made for people
Dreevoo.com | CONTRIBUTE | FORUM | INFO