0

On my form, a user can modify it, i.e. make it dirty, and then click the Cancel button.

The Cancel button's onClick() behavior is to change window.location.

However, when I press the "Cancel" button, I notice that the window.location only changes if I click "OK" (IE8) or "Leave this Page" (FF or Chrome). But, if I click "Cancel" (IE8) or "Stay on this Page" (FF or Chrome), then the actual window.location does not change.

How does this work?

EDIT including code:

function (buttonPressed) { // called when Cancel button is pressed
   window.location = ...;
}
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384

2 Answers2

1

As @マルちゃん だよ said, you can't force a redirect if the user doesn't allow it. It just won't happen. However, the question that needs to be asked is what your "Cancel" action does, and whether you actually need to use Javascript for it.

If you want the cancel button to reset the entire form, there are ways to do that, either

Alternatively, if cancelling is meant to take the user to a different location, then while the words may say cancel, the button is actually submitting the form and relocating them. So, make the button a type=submit, capture the fact that the form was cancelled (maybe even store it so the user can return to it later), and redirect them server side to the right page. This has the added benefit that you can track how many users are cencelling, and working when Javascript is turned off or unavailable.

  • Hi @Lego. When a user clicks my Cancel button, an IE (dirty form) prompt shows up - "Are you sure you want to navigate...". If the user clicks "OK", then I want the re-direct to occur. If the user selects "Cancel," I want the user to stay on the current page. This behavior currently works, but an apparently harmless IE8 script error occurs when clicking Cancel --> Cancel. Do I need to add JS code to handle the IE8 dirty form prompt's result? – Kevin Meredith Jul 08 '13 at 15:37
  • I don't understand - what doess "Cancel -> Cancel" mean? –  Jul 08 '13 at 23:17
  • I press cancel on my button, then press Cancel in the IE8 dialog, "are you sure you want to navigate away?" – Kevin Meredith Jul 09 '13 at 00:21
  • Yeah, you can't stop that dialog from popping up. Its a security flaw otherwise. –  Jul 09 '13 at 00:23
  • Are you sure? I thought `window.onbeforeunload = null` will disable it? Additionally, is the IE8 `unspecified error` an IE8 problem? How can I deal with it – Kevin Meredith Jul 09 '13 at 03:27
  • You've got some solutions and that question talks about when that function is overridden. Add some more information into the question that addresses the points I raised, otherwise it is hard to help any further. –  Jul 09 '13 at 03:30
0

What you are asking for would be a major security breach. I'm afraid you will never be able to do that.

You can, however, have control over child windows the parent has opened. Say you opened a popup with a parent window, that same parent window can have a button to close the child. Never the main window.

As for the "Cancel" event on that confirmation dialog, you could always handle it:

window.onbeforeunload = function () {
    if (confirm('Are you sure you want to leave this page?')) {
        return; // allow user to exit without further annoying pop-ups
    } else {
        // Handle the Cancel event
        // ...

        return "Do you really, really want to Exit?"; // Make the browser ask for confirmation again
    }
}

If the user does something to leave the page, then you can tell the browser to ask them if they are sure. You cannot silently prevent them. If the user wishes to leave the page, then they can do so (dodgy sites full of adverts and/or malware would love it to be otherwise, thankfully it isn't)

Bigger
  • 1,807
  • 3
  • 18
  • 28
  • I could always disable this dialog via `windows.onbeforeunload = null;`, right? http://stackoverflow.com/questions/11183682/how-to-prevent-are-you-sure-you-want-to-navigate-away-from-this-page-alert-on – Kevin Meredith Jul 08 '13 at 02:55