views:

3941

answers:

8

Does anyone know how to stop a page from reloading or navigating away?


jQuery(function($) {

    /* global on unload notification */
    warning = true;

    if(warning) {
     $(window).bind("unload", function() { 
      if (confirm("Do you want to leave this page") == true) {
       //they pressed OK
       alert('ok');
      } else {
       // they pressed Cancel
       alert('cancel');
       return false;
      }
     });
    }
});

I am working on an e-commerce site at the moment, the page that displays your future orders has the ability to alter the quantities of items ordered using +/- buttons. Changing the quantities this way this doesn't actually change the order itself, they have to press confirm and therefore committing a positive action to change the order.

However if they have changed the quantities and navigate away from the page I would like to warn them they are doing so in case this is an accident, as the changed quantities will be lost if they navigate away or refresh the page.

In the code above I am using a global variable which will be false by default (its only true for testing), when a quantity is changed I will update this variable to be true, and when they confirm the changes I will set it to false.

If warning is true and the page is unloaded, I offer them a confirmation box, if they say no they would like to stay on this page I need to stop it from unloading. return false isn't working, it still lets the user navigate away (the alerts are there for debugging only)

Any ideas?

+4  A: 

you want to use the onbeforeunload event.

scunliffe
@scunliffe - in jQuery 'unload' means 'onbeforeunload', that's what she's doing.
karim79
@karim79: no it doesn't. There isn't anything in jQuery that binds to the beforeunload function; "unload" binds to the "unload" event. Search the source if you don't believe me ;-)
NickFitz
A: 

Try using e.preventDefault() instead of returning false. 'e' would be the first argument to your unload callback.

MathieuK
@MathieuK - I voted up as I think you're right, but you should probably either 1)put in an example or 2)modify the code in the question to incorporate your suggestion before someone else comes along and does that ;)
karim79
preventDefault won't have any effect on the unload event - it isn't possible for a script to cancel unload, as that would allow a malicious site to hijack the browser window.
NickFitz
+10  A: 

onbeforeunload is the one you want; your function "should assign a string value to the returnValue property of the Event object and return the same string". Check the docs from Microsoft and Mozilla for details.

The string you return will be used by the browser to present the user with a custom confirm box, allowing them to refuse to stay there if they so choose. It has to be done that way to prevent malicious scripts causing a Denial-of-Browser attack.

NickFitz
+3  A: 

Thanks everyone, looks like it was indeed the onbeforeunload event we were after, its not entirely cross browser but it works in Firefox 3+ and Safari and all IE not Opera or Camino though, I don't think it is a standard event. Looks like it was implemented by IE and adopted by Mozilla and Safari, I haven't checked Google Chrome yet.

var warning = true;
window.onbeforeunload = function() { 
  if (warning) {
    return 'You have made changes on this page that you have not yet confirmed. 
    If you navigate away from this page you will loose your unsaved changes';
  }
}

The string as @NickFitz says displays in between the browsers' default text, if you just return false instead then Safari and IE display the word 'false'. So if you don't want a string to appear in the browser alert you need to return an empty string '' instead.

Natalie Downe
Yes, chrome supports it.
Crescent Fresh
You had two correct answers given to you before you posted this. Give one of them the green checkmark. Giving it to yourself is inconsiderate.
jcdyer
I agree with user 131084.
Blankasaurus
A: 

window.onbeforeunload = function() { if (warning) { return 'You have made changes on this page that you have not yet confirmed. If you navigate away from this page you will loose your unsaved changes'; } }

isnt supported in chrome, safari and opera

Isn't supported on Chrome? I'm pretty sure SO presents such a dialog to me when I entered something in the editor (I'm using Chrome 4.0.206.0 on Linux)
Joachim Sauer
A: 

Thanks @Natalie! Your solution worked like magic.

Peter Kahuria
A: 

@Natalie - GREAT! This was just what i was looking for!

misha
+1  A: 

This code warns as per Natalie's suggestion, but disables the warning if a form on the page was submitted. Uses JQuery.

var warning = true;
window.onbeforeunload = function() { 
  if (warning) {
    return "You have made changes on this page that you have not yet confirmed. If you navigate away from this page you will loose your unsaved changes";
  }
}

$('form').submit(function() {
   window.onbeforeunload = null;
});
crafter