Search code examples
javascriptjqueryace-editoronbeforeunload

checking the ace editor in the onbeforeunload to see if changes were made


I'm trying to use the $(window).on('beforeunload', function(){}); and editor.session.getUndoManager().isClean(); in the ace editor to check if the user made changes to a document but didn't click the submit button but for some reason it doesn't work Here's the code:

editor.on('change', function(Isclean ) {
        var Isclean = editor.session.getUndoManager().isClean();
    });
var submitting = false;

$(window).on('beforeunload', function(){
    if (submitting == false && Isclean == false) {
            return "You have made some changes in the editor.";
    };
});

But for whatever reason Isclean always returns a boolean of true.


Solution

  • In order to accomplish this task I did:

        $(window).bind("load", function() {
        editor.session.getUndoManager().markClean();
    });
    

    I was pre-loading some Json into the editor so unless i called editor.session.getUndoManager().markClean(); as the last thing to load (and it HAS to be the last thing to load), it would always boolean to false.