-1

When you typing something on facebook, and click refresh button or click go previous page button without save, facebook will warning you "you are not save the information, are you sure about that?" some like that.

How to detect those two event? unonload is not work in Chrome`, and facebook did detect real refresh event.

Chan
  • 1,947
  • 6
  • 25
  • 37

1 Answers1

1
window.onbeforeunload=function(){
    return 'Are you sure you want to exit?';
}

Note that this doesn't work with older versions of Opera. Newer ones use WebKit instead of Presto so you're all good.

Schien
  • 3,855
  • 1
  • 16
  • 29
  • Please *don't* use this code. This is not how you [properly set an event listener](http://stackoverflow.com/questions/6927637/addeventlistener-in-internet-explorer). – buschtoens Jan 20 '14 at 04:27
  • This event handler is not wrong when you have only one event to handle. Of course the proper method allow multiple listeners to subscribe to the same event. But in this context, we know what the objective is, and a direct assignment works in all browsers. Go ahead, use it, and let me know when the code fails, if it ever does. – Schien Jan 20 '14 at 04:35
  • SO is about teaching and learning. If you teach those beginners to use this method, they will continue to do so for other events and situations. And then things *will* go wrong. – buschtoens Jan 20 '14 at 04:44
  • I think our debate has presented both schools of thought, which is helpful to the individual who posted this question. I don't like how many programmers simply choose addEventListener because they're told that it's "proper" without questioning why. The "on" method works in all browsers, but can allow only one handler at a time. There is a risk of overriding existing handlers. var oldevent=window.onbeforeunload; window.onbeforeunload=function(){domything(); window.onbeforeunload=oldevent;} Better? – Schien Jan 20 '14 at 04:49