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
+17.5k Golang : Iterate linked list example
+29.6k Golang : How to get HTTP request header information?
+15.9k Golang : How to check if input from os.Args is integer?
+6.6k Fix sudo yum hang problem with no output or error messages
+11.1k Golang : Post data with url.Values{}
+14.5k Golang : Adding XML attributes to xml data or use attribute to differentiate a common tag name
+7.5k Golang : Test if an input is an Armstrong number example
+6.8k Golang : Find the shortest line of text example
+12.8k Golang : List objects in AWS S3 bucket
+5.4k Javascript : How to refresh page with JQuery ?
+11.2k Golang : Format numbers to nearest thousands such as kilos millions billions and trillions
+20.2k Golang : Secure(TLS) connection between server and client