CKEditor instance already exists

前端 未结 30 3434
刺人心
刺人心 2020-11-27 11:24

I am using jquery dialogs to present forms (fetched via AJAX). On some forms I am using a CKEditor for the textareas. The editor displays fine on the first load.

Whe

相关标签:
30条回答
  • 2020-11-27 11:53

    For ajax requests,

     for(k in CKEDITOR.instances){
        var instance = CKEDITOR.instances[k];
        instance.destroy()
     }
      CKEDITOR.replaceAll();
    

    this snipped removes all instances from document. Then creates new instances.

    0 讨论(0)
  • 2020-11-27 11:53

    I ran into this exact same thing and the problem was that the wordcount plugin was taking too long to initialize. 30+ seconds. The user would click into the view displaying the ckeditor, then cancel, thereby ajax-loading a new page into the dom. The plugin was complaining because the iframe or whatever contentWindow is pointing to was no longer visible by the time it was ready to add itself to the contentWindow. You can verify this by clicking into your view and then waiting for the Word Count to appear in the bottom right of the editor. If you cancel now, you won't have a problem. If you don't wait for it, you'll get the i.contentWindow is null error. To fix it, just scrap the plugin:

    if (CKEDITOR.instances['textarea_name']) 
    {
       CKEDITOR.instances['textarea_name'].destroy();
    }
    CKEDITOR.replace('textarea_name', { removePlugins: "wordcount" } );
    

    If you need a word counter, register for the paste and keyup events on the editor with a function that counts the words.

    0 讨论(0)
  • 2020-11-27 11:54

    You can remove any ckeditor instance by remove method of ckeditor. Instance will be id or name of the textarea.

    if (CKEDITOR.instances[instance_name]) {
        CKEDITOR.remove(CKEDITOR.instances[instance_name]);
    }
    
    0 讨论(0)
  • 2020-11-27 11:54

    To support dynamic (Ajax) loading of forms (without page refreshes between) which contain textareas with the same (same form is called again) or different ID's (previously unloaded form) and convert them to CKEditor elements I did the following (using the JQuery adapter):

    After the page has finished every Ajax call that delivers a textarea to be converted, I make a call to the following function:

    setupCKeditor()
    

    This looks like this (it assumes your textareas to be converted to RTE's have class="yourCKClass"):

        /* Turns textAreas into TinyMCE Rich Text Editors where
     * class: tinymce applied to textarea.
     */
    function setupCKeditor(){
    
        // define editor configuration      
        var config = {skin : 'kama'};
    
        // Remove and recreate any existing CKEditor instances
        var count = 0;
        if (CKEDITOR.instances !== 'undefined') {
            for(var i in CKEDITOR.instances) {
    
                var oEditor   = CKEDITOR.instances[i];
                var editorName = oEditor.name;
    
                 // Get the editor data.
                var data = $('#'+editorName).val();
    
                // Check if current instance in loop is the same as the textarea on current page
                if ($('textarea.yourCKClass').attr('id') == editorName) {
                    if(CKEDITOR.instances[editorName]) {
    
                        // delete and recreate the editor
                        delete CKEDITOR.instances[editorName];
                        $('#'+editorName).ckeditor(function() { },config);
                        count++;
    
                    }
                }   
    
    
            }
        }
    
        // If no editor's exist in the DOM, create any that are needed.             
        if (count == 0){
    
            $('textarea.yourCKClass').each( function(index) {
    
                    var editorName = $(this).attr('id');
                    $('#'+editorName).ckeditor(function() { $('#'+editorName).val(data); },config);
    
                });
    
        }
    
    
    }
    

    I should mention that the line:

    $('#'+editorName).ckeditor(function() { $('#'+editorName).val(data); },config);
    

    could (and should) be simply:

    $('#'+editorName).ckeditor(function() { },config);
    

    however I found that the editor would often show the correct content for a second after loading and them empty the editor of the desired content. So that line with the callback code forces the CKEditor content to be the same as the originating textarea content. Causes a flicker when used. If you can avoid using it, do so..

    0 讨论(0)
  • 2020-11-27 11:55

    The i.contentWindow is null error seems to occur when calling destroy on an editor instance that was tied to a textarea no longer in the DOM.

    CKEDITORY.destroy takes a parameter noUpdate.

    The APIdoc states:

    If the instance is replacing a DOM element, this parameter indicates whether or not to update the element with the instance contents.

    So, to avoid the error, either call destroy before removing the textarea element from the DOM, or call destory(true) to avoid trying to update the non-existent DOM element.

    if (CKEDITOR.instances['textarea_name']) {
       CKEDITOR.instances['textarea_name'].destroy(true);
    }
    

    (using version 3.6.2 with jQuery adapter)

    0 讨论(0)
  • 2020-11-27 12:00

    This is the fully working code for jquery .load() api and ckeditor, in my case I am loading a page with ckeditor into div with some jquery effects. I hope it will help you.

     $(function() {
                runEffect = function(fileload,lessonid,act) {
                var selectedEffect = 'drop';
                var options = {};
                $( "#effect" ).effect( selectedEffect, options, 200, callback(fileload,lessonid,act) );
            };
            function callback(fileload,lessonid,act) {
                setTimeout(function() {//load the page in effect div
                    $( "#effect" ).load(fileload,{lessonid:lessonid,act:act});
                     $("#effect").show( "drop", 
                              {direction: "right"}, 200 );
                    $("#effect").ajaxComplete(function(event, XMLHttpRequest, ajaxOptions) {
                        loadCKeditor(); //call the function after loading page
                    });
                }, 100 );   
            };
    
            function loadCKeditor()
            {//you need to destroy  the instance if already exist
                if (CKEDITOR.instances['introduction']) 
                {
                    CKEDITOR.instances['introduction'].destroy();
                }
                CKEDITOR.replace('introduction').getSelection().getSelectedText();
            }
        });
    
    ===================== button for call the function ================================
    
    <input type="button" name="button" id="button" onclick="runEffect('lesson.php','','add')" >
    
    0 讨论(0)
提交回复
热议问题