Forum Moderators: open

Message Too Old, No Replies

onbeforeunload event parameter and empty return value

         

Rain_Lover

7:26 am on Jul 30, 2014 (gmt 0)

10+ Year Member Top Contributors Of The Month



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) [msdn.microsoft.com]
  2. onbeforeunload event | beforeunload event JavaScript [help.dottoro.com]
  3. javascript - How can I override the OnBeforeUnload dialog and replace it with my own? - Stack Overflow [stackoverflow.com]

But on MDN [developer.mozilla.org] 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 '';

penders

2:07 pm on Aug 4, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



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


This is a reference to the event object which is implicitly passed to all event handlers (DOM Level 2). However, if you are not using it in the function definition (as in this example), then there is no real requirement to declare it in the function signature.

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


Does this work? I imagine browser security will kick in to prevent this.

Rain_Lover

5:57 pm on Aug 4, 2014 (gmt 0)

10+ Year Member Top Contributors Of The Month



Does this work?


Yes, it seems to be working.

Thanks for the answer! :)