views:

3643

answers:

8

Here in stackoverflow, if you started to make changes then you attempt to navigate away from the page, a javascript confirm button shows up and asks: "Are you sure you want to navigate away from this page?" blee blah bloo...

Has anyone implemented this before, how do I track that changes were committed? I believe I could do this myself, I am trying to learn the good practices from you the experts.

I tried the following but still doesn't work:

<html>
<body>
    <p>Close the page to trigger the onunload event.</p>

    <script type="text/javascript">
        var changes = false;        
    window.onbeforeunload =
            function()
            {
                if (changes)
                {
                    var message = "Are you sure you want to navigate away from this page?\n\nYou have started writing or editing a post.\n\nPress OK to continue or Cancel to stay on the current page.";
                    if (confirm(message)) return true;
                    else return false;
                }
            }
    </script>

    <input type='text' onchange='changes=true;'> </input>
</body>
</html>

Can anyone post an example?

Thanks in advance!

+3  A: 

There is an "onunload" parameter for the body tag you can call javascript functions from there. If it returns false it prevents navigating away.

stribika
Fortunately, this doesn't actually work. Otherwise I'd hate to visit a page containing `<body onunload="return false;">`.
Søren Løvborg
+1  A: 

When the user starts making changes to the form, a boolean flag will be set. If the user then tries to navigate away from the page, you check that flag in the window.onunload event. If the flag is set, you show the message by returning it as a string. Returning the message as a string will popup a confirmation dialog containing your message.

If you are using ajax to commit the changes, you can set the flag to false after the changes have been committed (i.e. in the ajax success event).

Kirtan
+2  A: 

You can add an onchange event on the textarea (or any other fields) that set a variable in JS. When the user attempts to close the page (window.onunload) you check the value of that variable and show the alert accordingly.

Makram Saleh
+5  A: 

To turn it on:

window.onbeforeunload = "Are you sure you want to leave?";

To turn it off:

window.onbeforeunload = null;

Bear in mind that this isn't a normal event - you can't bind to it in the standard way.

To check for values? That depends on your validation framework.

In jQuery this could be something like (very basic example):

$('input').change(function() {
    if( $(this).val() != "" )
        window.onbeforeunload = "Are you sure you want to leave?";
});
Keith
I have plenty of TBs in my webform, I wanna set a global boolean flag in the page, then with jquery walk thru all the inputs with type=text and set the onchange to change the flag.the use your function evaluating the flag?
Shimmy
That would be something like: $('input:text').change(function() { window.onbeforeunload = $('input:text[value!=""]').length > 0 ? "warning" :null; });
Keith
For a simple check, or you can add more complex validation on each change
Keith
A: 

WHat you want to use is the onunload event in javascript.

Here is an example: http://www.w3schools.com/jsref/jsref_onunload.asp

waqasahmed
+3  A: 

With JQuery this stuff is pretty easy to do. Since you can bind to sets.

Its NOT enough to do the onbeforeunload, you want to only trigger the navigate away if someone started editing stuff.

Sam Saffron
+1  A: 

It can be easily done by setting a ChangeFlag to true, on onChange event of TextArea. Use javascript to show confirm dialog box based on the ChangeFlag value. Discard the form and navigate to requested page if confirm returns true, else do-nothing.

simplyharsh
A: 

As Keith noted, you may use the onbeforeunload event handler to accomplish this. It's an IE-thing, quite non-standard.

Works in IE and Firefox. Doesn't work in Opera. Haven't tested Safari and Chrome.

See this blog entry for more information

Søren Løvborg
It works in WebKit based browsers (Safari and Chrome) too
Keith