Gmail: the Back button (vs. Close button)...onBeforeUnload?

A

allan.young

Hi,

When in gmail I'm composing a new email message and I bit the browser
back button, I get a dialog that says "Your message has not been sent.
Discard your message?"

But if instead of clicking Back, I hit the Browser Close button, I get
the above message, PLUS if I click "cancel" on that dialog, I get the
following message 'Are you sure you want to navigate away from this
page? Press "Cancel" to remain on this page. Press OK to continue, or
Cancel to stay on the current page.'

This latter dialog is likely the usual onBeforeUnload() dialog.

What I don't understand here is why this latter (likely:
unBeforeUnload) dialog doesn't appear in the case where the user
clicks "cancel" on the dialog shown when they have hit the Back
button. Both Back and Close trigger onBeforeUnload.

Any thoughts?

Regards,
Allan
 
T

Tom Cole

Hi,

When in gmail I'm composing a new email message and I bit the browser
back button, I get a dialog that says "Your message has not been sent.
Discard your message?"

But if instead of clicking Back, I hit the Browser Close button, I get
the above message, PLUS if I click "cancel" on that dialog, I get the
following message 'Are you sure you want to navigate away from this
page? Press "Cancel" to remain on this page. Press OK to continue, or
Cancel to stay on the current page.'

This latter dialog is likely the usual onBeforeUnload() dialog.

What I don't understand here is why this latter (likely:
unBeforeUnload) dialog doesn't appear in the case where the user
clicks "cancel" on the dialog shown when they have hit the Back
button. Both Back and Close trigger onBeforeUnload.

Any thoughts?

Regards,
Allan

The second window you see is the result of returning a value from the
onbeforeunload event handler. If the function you have defined as the
onbeforeunload handler returns ANYTHING (even null) that little window
will pop up.

Try it out.
 
T

Tom Cole

Hi,

When in gmail I'm composing a new email message and I bit the browser
back button, I get a dialog that says "Your message has not been sent.
Discard your message?"

But if instead of clicking Back, I hit the Browser Close button, I get
the above message, PLUS if I click "cancel" on that dialog, I get the
following message 'Are you sure you want to navigate away from this
page? Press "Cancel" to remain on this page. Press OK to continue, or
Cancel to stay on the current page.'

This latter dialog is likely the usual onBeforeUnload() dialog.

What I don't understand here is why this latter (likely:
unBeforeUnload) dialog doesn't appear in the case where the user
clicks "cancel" on the dialog shown when they have hit the Back
button. Both Back and Close trigger onBeforeUnload.

Any thoughts?

Regards,
Allan

Here's a good page that describes what's happening:

http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript
 
A

allan.young

Hi Tom,

Thanks for your reply.

What is still not clear to me, even after reading page you suggested,
is the handling of the case in GMail where the user has a dirty
document and hits the browser's Back button.

In this case, GMail shows what appears to be a GMail specific OK|
Cancel modal dialog. If the user hits OK, no followup dialog is shown,
so in this case an empty string is probably returned from their
onbeforeunload handler.

But, if the user hits Cancel, the browser does not navigate away from
the dirty document, and the default onbeforeunload query dialog
doesn't show either. So my question is, in this Cancel case, how is
Gmail prevent the navigation away from the current page without
showing this query dialog? What would need to be returned from their
onbeforeunload handler in order to achieve this?

Regards,
Allan
 
T

Tom Cole

Hi Tom,

Thanks for your reply.

What is still not clear to me, even after reading page you suggested,
is the handling of the case in GMail where the user has a dirty
document and hits the browser's Back button.

In this case, GMail shows what appears to be a GMail specific OK|
Cancel modal dialog. If the user hits OK, no followup dialog is shown,
so in this case an empty string is probably returned from their
onbeforeunload handler.

But, if the user hits Cancel, the browser does not navigate away from
the dirty document, and the default onbeforeunload query dialog
doesn't show either. So my question is, in this Cancel case, how is
Gmail prevent the navigation away from the current page without
showing this query dialog? What would need to be returned from their
onbeforeunload handler in order to achieve this?

Regards,
Allan

I'b be lying if I said this were anything more than a guess, but their
site uses frames. What I think you're seeing is cimbined behaviour
from onbeforeunload handlers for two different pages. My guess would
be that in scenario 1 (backbutton) you're seeing the handler for the
visible frame, and in scenario 2 (close) your seeing both the handler
from the visible frame, plus the one from the non-visible frame.
 
A

allan.young

Hi Tom,

Still, whether two frames are used or not, how can a page or frame
that handles onbeforeunload avoid showing the default browser dialog
and yet support cancelling of the unload? To my knowledge, the only
way to cancel the unload is to return a non-empty string in the
onbeforeunload handler, which causes the browser to show its dialog.
If you pass an empty string, the page will unload.

Regards,
Allan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top