CKEditor - destroy an instance when the DOM node has been deleted CKEditor - destroy an instance when the DOM node has been deleted ajax ajax

CKEditor - destroy an instance when the DOM node has been deleted


If you need to destroy the ckeditor object and the elemnets in the DOM AFTER an AJAX call, you can do it by setting a boolean parameter to the function call destroy(true). This way it wont try to update the DOM:

var editor = CKEDITOR.instances[name];if (editor) { editor.destroy(true); }CKEDITOR.replace(name);

I have wrote 2 functions to be able to control these things a bit better. Notice that I have declared a variable before these functions can be used, but there are much slicker ways, but this approach was good enough for the purpose I needed it(I use and need only one instance):

    if(typeof(editor) == 'undefined')        var editor=null;    function ck_delete(editor)    {        if(typeof(editor) != 'undefined' && editor!=null)            editor.destroy();    }    function ck_init(ck_inst_name)    {        var el_id=document.getElementById(ck_inst_name);        if(typeof(el_id) != 'undefined' && el_id!=null)        {            if(typeof(editor) == 'undefined' || editor==null)            {                editor=CKEDITOR.replace( ck_inst_name );            }            else            {                ck_delete(editor);                editor=null;                editor = CKEDITOR.replace( ck_inst_name );            }        }    }

I also check if a HTML element that should be replaced exists so I dont get an error message.


We had this problem integrating CKEDITOR in GWT, in a popup dialog. When the dialog was destroyed, CKEDITOR raised this error - "Cannot read property 'document' of null." The solution was to destroy CKEDITOR before closing the dialog. (We had to extend the GWT ckeditor class in order to override this - using the editor.destroy(true) syntax given by Erik - Thanks, Erik!)