Massassi Forums Logo

This is the static archive of the Massassi Forums. The forums are closed indefinitely. Thanks for all the memories!

You can also download Super Old Archived Message Boards from when Massassi first started.

"View" counts are as of the day the forums were archived, and will no longer increase.

ForumsDiscussion Forum → JavaScript assistance
JavaScript assistance
2007-02-25, 8:03 AM #1
I'm trying to write a simple script - a page is composed of a number of checkboxes. If a user clicks any of them, it changes a variable in JS that flags that there has been a change in the selection of checkboxes. When the user navigates away, I want it to check to see if a change has been made and, if so, ask the user to confirm that they want to leave. My code is as follows:

Code:
// This will ask the user, if changes have been made, if they're sure they want to navigate away
function changesMade() {

        if (isChanged == 1) {

                if (confirm('You have made changes to your selection here.  Are you sure you wish to navigate away from this page?')) {

                        return true;

                }

                else { return false; }

        }

        return true;

}


I've implemented it using <body onunload="return changesMade()">; however, when the user clicks "Cancel", it still navigates away from the page. I've tried implementing it with onbeforeunload="return changesMade()", but Firefox and IE implement it with their own annoying "are you sure you want to navigate away?" pop-ups. Any idea on how to stop the browser from navigating away from the page when the user clicks "Cancel"?
the idiot is the person who follows the idiot and your not following me your insulting me your following the path of a idiot so that makes you the idiot - LC Tusken
2007-02-25, 8:23 AM #2
I don't think you can... If you could control people going away from the page it'd be heavily abused.

Your best bet is just to do something like:

Code:
   if (confirm('You have made changes to your selection here.  Are you sure you wish to navigate away from this page?')) {

                        return true;

                }

                else { history.back(); }
TheJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJkWho
SaysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSaysNiTh
eJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSa
ysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJ
k
WhoSaysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSays
N
iTheJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJkW
2007-02-25, 8:34 AM #3
I finally just had to put it in the onClick event within the anchors. :/
the idiot is the person who follows the idiot and your not following me your insulting me your following the path of a idiot so that makes you the idiot - LC Tusken
2007-02-25, 9:46 AM #4
Strange. The onunload event is called, but not the changesMade function.

I will investigate further.

Also you can cause a user to not navigate away. GMail does it all the time.

[Edit: You must have a syntax error somewhere, check your error console. I got the message to fire... but it navigates away regardless of whether I click OK or Cancel.]

2007-02-25, 12:52 PM #5
In my case, The_MAZZTer, it would call changesMade() - my JS is syntactically fine, as far as I can tell.

However, in order for Firefox & IE to catch the return of false and halt the navigation away from the page, I have to use the onbeforeunload - using onunload, I've already began to navigate away from the page.

I really don't know why FF and IE implemented their own dialogs for onbeforeunload. :argh:
the idiot is the person who follows the idiot and your not following me your insulting me your following the path of a idiot so that makes you the idiot - LC Tusken
2007-02-25, 1:50 PM #6
Aha I was wondering if there was something like that, but of course the only docs I can find on JavaScript events are like 20 years old.

2007-02-25, 2:25 PM #7
have you checked this out?

http://www.4guysfromrolla.com/demos/OnBeforeUnloadDemo1.htm
TheJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJkWho
SaysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSaysNiTh
eJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSa
ysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJ
k
WhoSaysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSays
N
iTheJkWhoSaysNiTheJkWhoSaysNiTheJkWhoSaysNiTheJkW

↑ Up to the top!