4

I'm trying to use the javascript onbeforeunload event to ask the user if they want to exit the page, but I don't want the event to fire (EDIT: "the event" being the dialog box that pops up asking the user to click ok to leave the site or click cancel to stay on the current page) if the user hits the back button since they will be most likely be staying on my site.

So is there a way to tell if a user has hit the back button using javascript or PHP?

I've gotten a solution using a hidden iframe that only works in IE, but I need something that can work for Firefox, Chrome, and Safari if possible.

EDIT: My IE solution works because when the user hits the back button the iframe is sent back but the parent page remains at the same spot. From this I can tell that the user has indeed hit the back button, so I then use history.back(). This little hack doesn't work in any other browser (to my knowledge), so I'm looking for a cross-browser solution.

tl;dr I'm using window.onbeforeunload to pop up a dialog asking users if they want to leave my site or not. I don't want this to pop up when the user hits the back button. How can I tell that the user has hit the back button in their browser?

Thanks, Rick

4
  • What is the event that is firing? If you need to save info when the user hits the back button, couldn't you save some info in a cookie or HTML5 has the new "localStorage" and "sessionStorage" objects to use. w3schools.com/html5/html5_webstorage.asp Commented Dec 20, 2010 at 22:04
  • 1
    Solution that works in IE? How? Seems hacky Commented Dec 20, 2010 at 22:06
  • 3
    Simple solution: don't try to keep people on your site. It's interruptive to the user experience. That being said, it is ok to get a confirmation if they're leaving a page that hasn't been saved, or something similar. Commented Dec 20, 2010 at 22:55
  • Well, you can check the history.length but you won't know if the user clicked back or forward to get to your page. Commented Dec 22, 2010 at 14:46

3 Answers 3

7

Short answer:

No.

Long answer:

Noooooooooooooooooooooooooooo.

please don't try to keep users on your website unless you have a very good reason to. Saving form fields would be an example of a good use. Checking if they're moving on to another website would be a bad use.

People don't travel from page-to-page as much as they did in the early days of the web. Instead they use google and social networks to find interesting pages, and consume separate distinct pieces of information.

Sign up to request clarification or add additional context in comments.

Comments

3

You can't know in advance on which page your user will go when he leaves your page. You can't even get the URLs in its current history.

I see no solution to your problem and I doubt there's one, sorry.

3 Comments

You can't always know in advance, but you can sometimes have a pretty good idea. For example, a mousedown on a link is a pretty good indication that someone is going to allow mouseup on the link and follow it.
Well, yes, that's tricky, but you can know when an user will leave your website attaching a function to the onclick event of an external link for example. But when he doesn't click on a link, you can't know whether he hits the back button, closes his browser, or enters a new URL in his browser directly.
See above edits for my solution to determining when the user hits the back button in Internet Explorer. I'm looking for another way to indicate a back button hit in browsers other than IE.
0

If you don't want anything to happen when the user clicks the back button, then you don't necessarily need to determine if the back button has been hit.

Your goal is to determine who will "most likely be staying on [your] site," and create an extra step for everyone who wants to leave. You're trying to interrupt and override the user's expectations of how his browser will behave.

If you really want to do this, have event listeners for all unload events that aren't triggered by the back button: every link on your page, closing the window, etc. It won't be easy, and you won't be able to catch all events. But you're going to be pissing people off unless you have a good reason for doing this, so if it's really important then put the extra effort in.

tl;dr: Add event listeners to everything that isn't the back button and bring up the dialog in the callback function. It will piss people off, though.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.