Javascript : Prompt confirmation before exit
There are times when I typed some words into Facebook comment fields and then somehow got distracted to do something else only to come back to my screen later...without realizing that I suppose to complete my reply and accidentally try to close the browser.
Facebook's Javascript will block my attempt to close the window and reminds me that I will lose all the data if I don't save it.
In this tutorial, we will try to achieve the following:
1. Create a form with textarea and submit button.
2. Add Javascript to detect textarea value and prompt exit confirmation.
3. Enhance the script exclude certain elements.
1. Create a form with textarea and submit button.
<form action="http://target-url" method="post">
<textarea id="target-textarea" name="answer" rows="15"></textarea>
<button id="submit" >Post Answer</button>
</form>
2. Add Javascript to detect textarea value and prompt exit confirmation.
<script> window.onbeforeunload = confirmExit;
function confirmExit() { // check to see if the ANSWER textarea have value or not. // prompt user to see if she/he wanted to post the answer before exiting.
if ($("#target-editor-with-help-button").val() != '') { // if the textarea value is not empty. return "You have't finished your post yet. If you have made any changes to the text field without clicking the Submit button, your changes will be lost."; };
} </script>
Test out the page, close the browser window and see how the prompt goes. You will notice that even if you click on the Submit button, the javascript will still prompt you. This can be very annoying to the end user as clicking on the submit button is the expected behavior to proceed to the next page.
We will enhance our javascript to handle this annoyance, but adding a global variable NoNeedConfirm
<script>
var NoNeedConfirm = false; // add this
window.onbeforeunload = confirmExit;
function confirmExit()
{
// check to see if the ANSWER textarea have value or not.
// prompt user to see if she/he wanted to post the answer before exiting.
// if the textarea value is not empty and needToConfirm is true
if (($("#target-editor-with-help-button").val() != '') && (NoNeedConfirm)) { // and new variable to check
return "You have't finished your post yet. If you have made any changes to the text field without clicking the Submit button, your changes will be lost.";
};
}
</script>
and in the form itself, add this line onclick="NoNeedConfirm = true;"
<form action="http://target-url" method="post">
<textarea id="target-textarean" name="answer" rows="15"></textarea>
<button id="submit" onclick="NoNeedConfirm = true;" >Post Answer</button>
</form>
The new enhancement should do the trick. Hope you may find this tutorial useful.
By Adam Ng
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+10.5k Golang : Select region of interest with mouse click and crop from image
+5k Linux/Unix/MacOSX : Find out which application is listening to port 80 or use which IP version
+10.1k Golang : How to profile or log time spend on execution?
+5.7k Unix/Linux : How to test user agents blocked successfully ?
+4.7k JavaScript: Add marker function on Google Map
+11.1k Golang : Fix - does not implement sort.Interface (missing Len method)
+14.8k Golang : Basic authentication with .htpasswd file
+50.9k Golang : Disable security check for HTTPS(SSL) with bad or expired certificate
+12.2k Golang : Display list of countries and ISO codes
+40k Golang : UDP client server read write example
+30.8k error: trying to remove "yum", which is protected
+8k Golang : Get all countries phone codes