determine cause of onbeforeunload

E

Eric Robishaw

If I want to show a closing message ONLY if the user is trying to
close the browser, I need to determine the cause of onbeforeunload
being called.

The following attempts do NOT work:
checking document.activeElement for the BODY tag
setting flags in window.onbeforeunload/onfocus or body.onactivate,
body.ondeactive

Any ideas?

Thanks
Eric
 
R

Randy Webb

Eric said:
If I want to show a closing message ONLY if the user is trying to
close the browser, I need to determine the cause of onbeforeunload
being called.

You can't. Script can only tell that I left, not how I left. And even
determining that I left is not always reliable.
The following attempts do NOT work:
checking document.activeElement for the BODY tag
setting flags in window.onbeforeunload/onfocus or body.onactivate,
body.ondeactive

Any ideas?

Why do you need a "closing message" only if I am closing the browser? I
am leaving the site....
 
M

Martin Honnen

Eric said:
If I want to show a closing message ONLY if the user is trying to
close the browser, I need to determine the cause of onbeforeunload
being called.

There are events fired when someone clicks a link or a submit button of
a form so you can certainly determine such cases and in that case disable
window.onbeforeunload = null
or set a flag e.g.
var linkClicked = false;

<a href="whatever.html"
onclick="window.linkClicked = true;
return true;">link</a>
and in you onbeforeunload handler you check
if (linkClicked) {
// set the returnValue here if wanted or unset it if wanted
}
 
E

Eric Robishaw

If the browswer is closing I need to warn the user.. giving them a
chance to save the data changes.

If, however, we're going to a link on the same site, I'll save the
changes automatically on the postback... thus making a notification to
the user not only unnecessary, but annoying.

Thanks
Eric
 
L

Lee

Eric Robishaw said:
If the browswer is closing I need to warn the user.. giving them a
chance to save the data changes.

If, however, we're going to a link on the same site, I'll save the
changes automatically on the postback... thus making a notification to
the user not only unnecessary, but annoying.

[Please don't top-post. Add your response *after* what
you're replying to, so it can be read top to bottom.]


Randy is pointing out that you've overlooked the other option.
What if the user is not closing the browser and is not going
to a link on the same site?

And you certainly can't detect the fact that they've gotten
sleepy and have decided to turn the computer off and go to bed,
without closing the browser.

The answer is that either your users understand web browsers
well enough to know that they haven't saved their entries
unless they've hit a submit button, or a web browser is not
an appropriate user interface for them to be using.
 
K

kaeli

If the browswer is closing I need to warn the user.. giving them a
chance to save the data changes.

If, however, we're going to a link on the same site, I'll save the
changes automatically on the postback... thus making a notification to
the user not only unnecessary, but annoying.

This is not acceptable behavior for an internet application - there are
too many ways a user can leave the site besides closing the browser,
plus some users don't have javascript enabled at all. What should happen
if the user grabs a URL from the favs? What if they type it? What if
they click a link in mail and it re-uses the window (mine does that)?
What if the browser crashes? What if the next version of Mozilla lets
users disable this feature (onunload)?

For an intranet application, it is acceptable, but still unnecessary if
the application is programmed properly.

One way to handle this is by storing data in active sessions. If the
user doesn't save the data, it is autosaved (or you notify the user via
mail or some such if possible) when the session expires (sessions expire
a certain amount of time after the browser is closed). A good example of
this is the shopping cart at amazon (I think that's where it was). If
you put stuff in your cart, but don't check out, they send you a mail
asking you if you still wanted the items and giving you a chance to
still buy them. If you don't respond in a certain amount of time, the
items are removed from the cart.

--
 

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

Forum statistics

Threads
473,996
Messages
2,570,238
Members
46,826
Latest member
robinsontor

Latest Threads

Top