sven30
sven30

Reputation: 583

CKEditor always alerts when exiting before saving even when submitting changes

I'm using the checkdirty function for checking my ckeditor for any changes before exiting. It's working ok but the alert comes up even when I submit the changes. What code would I change and where to get this submit button to skip the check and just save the changes?

Here's my code.

for ( var name in CKEDITOR.instances ) {
    if(submit_it)
        return false;

    if ( CKEDITOR.instances[ name ].checkDirty() ) {
        return evt.returnValue = "You will lose the changes made in the editor.";
    }
}
}

var submit_it = false;

if ( window.addEventListener )
    window.addEventListener( "beforeunload", beforeUnload, false );
else
    window.attachEvent( "onbeforeunload", beforeUnload );

Here's my javascript code in the head.

var warn_on_leave = false; 

CKEDITOR.on('currentInstance', function() {                
    try { 
        CKEDITOR.currentInstance.on('key', function() {        
            warn_on_leave = true; 
        }); 
    } catch (err) { }                                        
});

$(document.activeElement).submit(function() {   
    warn_on_leave = false; 
});

$(window).bind('beforeunload', function() { 
    if(warn_on_leave) { 
        return 'Attention: Your text has not been saved!'; 
    } 
}); 

Here's my submit button:

<input type="image" src="button_update.gif" border="0" alt="Update" name="udpate" title=" Update ">

Upvotes: 2

Views: 1105

Answers (1)

Jani
Jani

Reputation: 349

I was facing the similar problem and after Googling for 2 hours, I figured out a solution, what I have done is registered an event before the save command is executed:

var saved = false;

CKEDITOR.replace('#myTextArea', options);

CKEDITOR.plugins.registered['save'] = {
    init: function (editor) {
        editor.on( 'beforeCommandExec', function(event)
        {
            if (event.data.name === 'save') {
                saved = true;
            }
        });
    }
};

$(window).on('beforeunload', function () {
    if (saved === false) {
        if (CKEDITOR.currentInstance.checkDirty() === true) {
            return 'You have some unsaved changes on this page, are you sure you want to leave?';
        }
    }
});

Though this is very old question, I think my answer will help some one.

Upvotes: 2

Related Questions