19

I'm looking to implement a warning if the user attempts to leave the order process before it's completed in any fashion other then of course following the payment button.

Something like this:

<script type="text/javascript">
window.onbeforeunload = function(){
return 'You must click "Buy Now" to make payment and finish your order. If you leave now your order will be canceled.';
};
if document.getElementsByClassName('eStore_buy_now_button').onclick = function(){
};
</script>

I'm sure that's detrimentally wrong in a few ways, but it's the best way I can illustrate what I'm trying. And I understand some browsers will display default text instead of the actual warning I've written, that's fine.

A few notes, I'd rather use plain old JS instead of loading up jQuery for just this one simple task. There are no settings on the page so it's a simple leave page or click "Buy Now" operation.

UPDATE:

I assure you it's not for my sake, it's for the user's sake. Although it's explicitly explained (what to do), I think user's are jumping the gun and leaving before the process is truly finished out of an instant gratification, ignore the messages kind of mentality. It's a simple 2-step process, they submit the details for the project and then make payment. For whatever reason they're submitting details and then not following through with payment about 50% of the time. And then they'll follow up "So, are you working on the project or what?" and then I have to explain "You never finished your order." They follow up with a "Whoops, here ya go."

Unfortunately, I would chalk this up as marketing and web design 101. Rule #1, people are dumb. Not to be taken in a rude or pessimistic sense. Basically, the idea is assume everyone is dumb in your design, instruction so that you make something so easy a five-year-old can do it. I totally agree with not holding users hostage. But this page is ONLY reached in the middle of their intended order process that THEY initiate (this page will never be reached in a browsing sort of way). So I think it's a pretty legitimate use case where you're saving a common user mistake from themselves. A demographic of customers that are not tech-savvy, so they honestly need such guidance.

  • You can't do that. You just can suggest the user to do that – Danilo Valente Jun 17 '12 at 23:43
  • @MarkLinus He can do that I realize it is not the best idea as the user will either kill the entire page in a way that will destroy the session or get very annoyed but it is possible and nothing illegal about telling the user not to leave the page as their transaction is incomplete. – gabeio Jun 18 '12 at 00:47
  • Have a look @ http://stackoverflow.com/questions/1244535/alert-when-browser-window-closed-accidentally – PHP Ferrari Feb 18 '14 at 10:46

2 Answers2

27
document.querySelector('.eStore_buy_now_button').addEventListener("click", function(){
    window.btn_clicked = true;      //set btn_clicked to true
});

window.onbeforeunload = function(){
    if(!window.btn_clicked){
        return 'You must click "Buy Now" to make payment and finish your order. If you leave now your order will be canceled.';
    }
};

This will alert the user whenever the page unloads (eg leaving the page) until btn_clicked is set to true.

DEMO: http://jsfiddle.net/DerekL/GSWbB/show/

Derek 朕會功夫
  • 92,235
  • 44
  • 185
  • 247
  • Thanks, I'll give this a try in the morning. –  Jun 18 '12 at 07:42
  • Works great. Thank you. A nice little extra/tip is that this won't effect new window `target="_blank"` links. So, if you're using this on a settings page and need to have ? links next to form fields for extra info the user can pop those open without warning. _Make sure to load the JS after your form for anyone else that stumbles upon this solution and might not know that._ –  Jun 18 '12 at 19:25
  • 2
    Took me a long time to find this one. Thank you. – khaverim Apr 25 '13 at 19:09
  • 1
    "Make sure to load the JS after your form..." -- What you really are concerned with is executing the JS after your form is loaded, so if you are using jquery, for instance, you could just wrap it in `$(document).ready()` – Isaac Betesh Jun 19 '14 at 21:00
0

Don't do it.

There is a fine line in terms of usability - on one hand sometimes I may have intended to place an order but accidentally left the page; on the other hand it could get annoying pretty quickly. When abrowser is set up to save previous session (i.e. reopen tabs on next launch) and one page behaves this way, you'll end up with only that tab re-opened next time (confirmed on Mac Safari), discarding the rest of the tabs. They'll not be buying from you again!

I'd suggest you make it clear to the user by means of inline messages that the order has not been submitted yet and they still need to confirm their action, but if they were to accidentally navigate away you should make it easy to pick up where they left off. Would be fairly trivial to store such info in a cookie so that on subsequent page visits the user would be prompted with "you have an incomplete order for ..., would you like to finish it now?"

As an alternative, you could use rely on an inactivity alert (think of online banking prompting you when your session is about to expire) to bring the user back to the "complete order" page if they get distracted.

If you are certain you want to rely on this event, the answers to this question may provide better insight. Basically, the functionality or its implementation beyond a basic text warning should not be relied onto because of inconsistent (?) implementation across browsers as well as possibility of having it blocked by the user.

Another update:

Prompted by Derek's comment on this approach being used by Gmail etc., I've come across an article suggesting you stick with onunload instead and rely on AJAX calls to save the state of the page - which backs my thoughts on allowing the user to pick up where they left even if the javascript event is never triggered.

Community
  • 1
  • 1
Oleg
  • 24,465
  • 8
  • 61
  • 91
  • 3
    Why not use it? Google uses it all the time, for example, Gmail is one of them. – Derek 朕會功夫 Jun 18 '12 at 00:50
  • @Derek: touche, they are somehow completely unobtrusive though, I'll check it out! (you're talking about unsaved drafts etc, right?) – Oleg Jun 18 '12 at 01:06
  • maybe it's indicative of further adjustments needed in other areas? It's hard to guess without seeing the form in question, but maybe the process is not clear enough *for the users*. I mean, if you have a button called *complete order* that leads to this particular question, it could be interpreted as *complete order and take me to an "fyi confirmation screen"*. I assume it's not that obvious but a URL would help better judge the situation – Oleg Jun 18 '12 at 10:02
  • @o.v. - Trust me, it's more than clear. It's nothing new that people will not listen, follow instruction etc etc on a website. That's why we have form validation (besides spam) and legitimate use cases for "Leave Page" warnings and other deterrents/fail-safes. It states clearly that they're submitting details and that on the following page they will need to click "Buy Now". Then on the "Buy Now" page it states in bold, "YOUR ORDER IS NOT COMPLETE YET - PLEASE CLICK "BUY NOW" TO FINISH ORDER". So, although you make a few valid points this is indeed absolutely a legitimate exception. –  Jun 18 '12 at 19:09
  • ...also, besides all the additional instruction the initial form submit button reads as "Continue". I can't imagine someone misconstruing that as the conclusion to the order process. I think it indicates my theory being more likely. That people are simply concerned/excited with entering their details and do so with a certain tunnel vision that blocks everything else out. And for the submit button they're obviously clicking it because it's clearly the submit button (muscle memory). And then as soon as their details are sent they're bouncing. I'll check my GA stats. –  Jun 18 '12 at 19:37
  • good to know you've looked into this, I didn't mean to imply otherwise. UX is tough! SO also has a dedicated user experience community at http://ux.stackexchange.com if you wanted more constructive feedback – Oleg Jun 18 '12 at 21:54
  • 1
    @o.v. - No worries, didn't think you did. Just wanted to make a thorough case for a legitimate use case for the benefit of others. You might want to change "Don't do it." to something along the lines of "Try and avoid such measures as much as possible." That would be more accurate and better information/advice and I fully agree. –  Jun 19 '12 at 01:49