Here is my code:
// confirm alert before close page
window.onbeforeunload = function(evt) {
var el = document.getElementById("qandatextarea");
if( el && el.value && !DontAskBeforeExit){
var message = 'are you sure you want to exit this page?';
if (typeof evt == 'undefined') {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}
}
Please focus on this line:
if( el && el.value && !DontAskBeforeExit){
Well there is three conditions:
el
: checks whether is that textarea exists in the DOM ?el.value
: checks has that textarea any value? (in other word, isn't that textarea empty?)!DontAskBeforeExit
: it is a variable which is either0
or1
. When the content of that textarea is stored into localStorage, it is1
, otherwise it is0
.
Ok, now I need to one more condition. I need to check "is the page submitting or exiting"? I mean, I don't need to show that pop-up alert if the user is sending a post (submitting). How can I do that?