1

Q1: I have seen the following code in most sources:

window.onbeforeunload = function () {
    return 'You might lose your data!';
};

For example:

  1. beforeunload | onbeforeunload event (Internet Explorer)
  2. onbeforeunload event | beforeunload event JavaScript
  3. javascript - How can I override the OnBeforeUnload dialog and replace it with my own? - Stack Overflow

But on MDN the function has an extra parameter: (e). Why is that, what is the difference, and which is right?

Q2: It seems that any browser has a default confirmation message that you cannot customize. Chrome, for example, displays:

Are you sure you want to leave this page?

Can I content myself to this message and leave the return value empty:

return '';
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Mori
  • 8,137
  • 19
  • 63
  • 91
  • All event handlers have an `event` parameter, it contains the details of what triggered the event. – Barmar Jul 30 '14 at 06:58
  • And is it necessary to use it in this case? – Mori Jul 30 '14 at 06:59
  • 1
    It's there if your application wants to use it. If you don't need it, ignore it, just like the examples do. – Barmar Jul 30 '14 at 07:00
  • If you are happy with the default message, use `event.preventDefault();` instead. I personally think it looks cleaner – CodingIntrigue Jul 30 '14 at 07:01
  • There's probably nothing useful in it for this particular event. It's just part of the common event handling protocol. – Barmar Jul 30 '14 at 07:01
  • @RGraham: It doesn't seem to work in Chrome: [**MDN**](https://developer.mozilla.org/en-US/docs/Web/Events/beforeunload). – Mori Jul 30 '14 at 08:35

0 Answers0