0

I have a textarea in form. I want to show confirmation box when user close the window without save the content.

I have a code

var myEvent = window.attachEvent || window.addEventListener;
var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compitable
myEvent(chkevent, function(e) { // For >=IE7, Chrome, Firefox
    var confirmationMessage = ' ';  // a space
    (e || window.event).returnValue = confirmationMessage;
    return confirmationMessage;
});

it works well. But i am getting the same confirmation box when i submit save button. How can i find whether submit button clicked or not?.

Thanks

Durgesh Chaudhary
  • 1,075
  • 2
  • 12
  • 31
Rajaraman
  • 1,637
  • 5
  • 19
  • 24
  • Use [removeEventListener](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget.removeEventListener) when the save button is clicked – CodingIntrigue Mar 27 '14 at 13:18
  • just initialize some flag on submit click and check – Dimag Kharab Mar 27 '14 at 13:34
  • Hi thanks for the replies. I have tried all these ideas. But it does not work. I want to remove event Listener when i click submit button. So it should not ask any confirmation when i submit. – Rajaraman Mar 27 '14 at 14:33
  • i found the solution here. thanks for the replies http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo2.htm – Rajaraman Mar 28 '14 at 09:16

1 Answers1

0

Maybe a little tweaky, but you could create an event handler on the submit of the form and set a variable there to ignore the message to pop up.

<form onsubmit="disableMessagePopup()">
  ...
</form>
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325