onbeforeunload - ask user if unsaved changes

Added by ----- -----. 11 months ago

I hope, that is the last time I have to annoy you with my questions...

You have already helped me creating the autosave function, now I want to notify the user "onbeforeunload" if there are unsaved changes:

var content = $('#editor').elrte('val');
    setInterval("content = updateAll( content );", 4  * 60 * 1000 ); // updateAll returns the last saved content
    function onClose( oldContent )
    {
    if ( oldContent != $('#editor').elrte('val') )
        {
            return "You have unsaved changes, here will appear the default dialog of the browser";
        }
    else
        return false;
    }
window.onbeforeunload = onClose(content);

But it is not working, the message never appears with this method.

Replies (5)

RE: onbeforeunload - ask user if unsaved changes - Added by Dmitry Levashov 11 months ago

window.onbeforeunload = funtion() { onClose(content); } ?

RE: onbeforeunload - ask user if unsaved changes - Added by ----- -----. 11 months ago

Was that a question or a suggest, if it was a suggest:
It makes no difference (yes, I corrected funtion to function, but it didn't help).

Any other ideas?

RE: onbeforeunload - ask user if unsaved changes - Added by Dmitry Levashov 11 months ago

use firebug to debug values in you sript

RE: onbeforeunload - ask user if unsaved changes - Added by ----- -----. 11 months ago

I could solve two problems with Firebug:
windows.onbeforeunload mustn't have a parameter, so I deleted it and it's working now.

function onClose()
    {
        if ( content != $('#editor').elrte('val') )
            return "Sie haben ungespeicherte Änderungen, wollen Sie wirklich fortfahren?";
    }
window.onbeforeunload = onClose;

I think such a functio should be in elRTE by default, it should be really easy to integrate.

The second you can see here:
http://elrte.org/redmine/boards/4/topics/3089?r=3128

RE: onbeforeunload - ask user if unsaved changes - Added by ----- -----. 11 months ago

Can I suppress this message, if the user just uses the "save" button?

Now I send the formular using AJAX in combination with a <input type="button" onclick="function()" />

(1-5/5)