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
+16.4k Convert JSON to CSV in Golang
+12.1k Golang : Read from buffered reader until specific number of bytes
+26k Golang : Record voice(audio) from microphone to .WAV file
+5.9k Mac OSX : Find large files by size
+17.4k Golang : Check whether a network interface is up on your machine
+15.6k Golang : Capture stdout of a child process and act according to the result
+4.8k Golang : Use NLP to get sentences for each paragraph example
+7.6k Golang : Gaussian blur on image and camera video feed examples
+36k Golang : Read a text file and replace certain words
+3.9k Which content-type(MIME type) to use for JSON data
+6.1k Javascript : How to get JSON data from another website with JQuery or Ajax ?
+16k Golang : How to save log messages to file?