Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

onbeforeunload: close vs. back button

Status
Not open for further replies.

Morphos

Technical User
Feb 9, 2010
4
0
0
SK
Hi,
I have the following problem:
When I use this script it pops up both if a user tries to close the window and also if he presses the back button. Please tell how to show another window or do another action when the back button is pressed? I know it is possible although a lot of people say it's not.
Code:
<script type="text/javascript" type="text/javascript">
window.onbeforeunload = confirmExit;
  function confirmExit()
  {
    return "blabla";
  }
 </script>
 
Hi

Morphos said:
when the back button is pressed
And what if the user
[ul]
[li]executes History menu's Back command[/li]
[li]executes page context menu's Back command[/li]
[li]presses Alt-left arrow keys[/li]
[li]chooses the previous page form the Recent pages list[/li]
[li]performs a mouse gesture ( drag left in my FireGestures )[/li]
[li]hits a shortcut ( the divide key by default in Mousless Browsing )[/li]
[li]executes a bookmarklet to navigate back[/li]
[/ul]
All the above applies to FireFox and the list is not complete. Other browsers may provide similar or different ways to navigate. And you probably want to catch them all. I wish you luck.

Feherke.
 
Feherke, go to your gmail.com account start writing a message and try:
1. click the back button
2. go close the window

You will get 2 different popups and that's what I need as well. Or even better I'd like to have no popup when the user wants to go back and anything else except going to close the window or navigate from my website.
 
No I don't.
Go to your gmail.com account, then go to create new email, type something int the text field of the email. And now if you click the back button this message pops up: Your e-mail hasn't been sent, Delete message?
On the other hand if you go close the window it says: your message hasn't been sent do you do you want to navigate from this page?

It's not just me check out this guy's post:
 
Hi

Ah, found them. The messages you mentioned are not implemented in GMail's Basic HTML interface.

Deducing from
[ul]
[li]the message texts[ul]
[li]the browser's standard message is displayed on close, reload, following link[/li]
[li]custom message displayed on navigate back and navigate forward[/li]
[/ul][/li][li]inconsistency across browsers[ul]
[li]no message on close, reload, following link in Presto, WebKit, KHTML[/li][/ul][/li]
[li]the relatively big number of used [tt]iframe[/tt]s[/li]
[/ul]
I would say
[ul]
[li]they use the browser's standard unload message on the main document ( see faq216-6708 )[/li]
[li]they use an [tt]iframe[/tt] to catch navigation attempts[ul]
[li]after loading the main document, script loads a new document into the [tt]iframe[/tt][/li]
[li]when navigating back or forward, Gecko and Presto reverses the last navigation - so the back forward will be performed in the [tt]iframe[/tt][/li]
[li]main document catches the [tt]unload[/tt] performed inside the [tt]iframe[/tt] and displays a [tt]confirm()[/tt] message[ul]
[li]if the user presses Ok, the main document's [tt]onbeforeunload[/tt] is unset and a [tt]history.back()[/tt] is performed[/li]
[li]if the user presses Cancel, the content of the [tt]iframe[/tt] is set back to be able to catch further navigation attempts[/li]
[/ul][/li][/ul][/li]
[/ul]
Well, in very big steps. Of course, I may be wrong. I have no time to debug GMail's biggest [tt]iframe[/tt]'s 713 Kb generated source. Anyway, I still strongly believe that is impossible to tell back navigation and window closing apart.

Feherke.
 
Thank you Feherke, I appreciate your help.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top