The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> onbeforeunload event parameter and empty return value
RainLover
post Jul 30 2014, 02:40 AM
Post #1


Advanced Member
****

Group: Members
Posts: 216
Joined: 16-November 09
Member No.: 10,346



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

CODE
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:

QUOTE
Are you sure you want to leave this page?

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

CODE
return '';
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jul 30 2014, 05:18 AM
Post #2


.
********

Group: WDG Moderators
Posts: 9,656
Joined: 10-August 06
Member No.: 7



QUOTE(RainLover @ Jul 30 2014, 09:40 AM) *

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

Not sure, but I don't think that it has to be called exactly "e", and in the above case I think it's not even used for anything. See http://quirksmode.org/js/introevents.html#link10 (and further down that page) for usage examples.

When using try/catch statements I recall it's mandatory for the catch statement to work: https://developer.mozilla.org/en-US/docs/We...nts/try...catch unsure.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
RainLover
post Jul 30 2014, 08:10 AM
Post #3


Advanced Member
****

Group: Members
Posts: 216
Joined: 16-November 09
Member No.: 10,346



QUOTE(Christian J @ Jul 30 2014, 05:18 AM) *


Yes, it seems unnecessary.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Jul 30 2014, 09:09 AM
Post #4


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,730
Joined: 9-August 06
Member No.: 6



Where do you find that "e"? I don't see it at MSDN. I know I need new glasses, but I really don't find it. It's not in the sample code, that I'm sure of. blush.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jul 30 2014, 01:16 PM
Post #5


.
********

Group: WDG Moderators
Posts: 9,656
Joined: 10-August 06
Member No.: 7



It's at MDN, not MSDN:

CODE
window.onbeforeunload = function(e) {
  return 'Dialog text here.';
};

cool.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Jul 30 2014, 01:30 PM
Post #6


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,730
Joined: 9-August 06
Member No.: 6



Yeah, I figured as much, but where on the MSDN page? The only code sample I see is this.

CODE
<!DOCTYPE html>
<html>
<head>
<script>
  function closeIt()
  {
    return "Any string value here forces a dialog box to \n" +
         "appear before closing the window.";
  }
  window.onbeforeunload = closeIt;
</script>
</head>
<body>
  <a href="http://www.microsoft.com">Click here to navigate to
      www.microsoft.com</a>
</body>
</html>

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jul 30 2014, 02:21 PM
Post #7


.
********

Group: WDG Moderators
Posts: 9,656
Joined: 10-August 06
Member No.: 7



QUOTE(pandy @ Jul 30 2014, 08:30 PM) *

Yeah, I figured as much, but where on the MSDN page?

pandy... rolleyes.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Jul 30 2014, 02:47 PM
Post #8


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,730
Joined: 9-August 06
Member No.: 6



WHAT? angry.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Jul 30 2014, 02:48 PM
Post #9


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,730
Joined: 9-August 06
Member No.: 6



Uh-oh! blush.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
pandy
post Jul 30 2014, 02:52 PM
Post #10


🌟Computer says no🌟
********

Group: WDG Moderators
Posts: 20,730
Joined: 9-August 06
Member No.: 6



Don't you think it's just to show you can pass a parameter that way? It is odd though since they don't seem to do it for the other events I checked.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Christian J
post Jul 30 2014, 04:16 PM
Post #11


.
********

Group: WDG Moderators
Posts: 9,656
Joined: 10-August 06
Member No.: 7



Maybe someone just made a typo, can happen to the best of us. happy.gif
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
2 User(s) are reading this topic (2 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 25th April 2024 - 09:04 AM