javascript 如何确定是否加载了 CKEditor?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3447803/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 01:12:20  来源:igfitidea点击:

How to determine if CKEditor is loaded?

javascriptckeditor

提问by friedkiwi

How do I find out if CKEditor is loaded? I've looked through the API docs, but could only find the loaded event. I want to check if CKEditor is loaded, because if I load it a second time, my textareas disapears.

如何确定 CKEditor 是否已加载?我查看了 API 文档,但只能找到加载的事件。我想检查 CKEditor 是否已加载,因为如果我第二次加载它,我的 textareas 就会消失。

回答by Kees C. Bakker

The loadedevent didn't work for me. instanceReadyworked:

loaded活动对我不起作用。instanceReady工作:

CKEDitor_loaded = false;

CKEDITOR.on('instanceReady', function(){ CKEditor_loaded = true; }); 

回答by Alex

var waitCKEDITOR = setInterval(function() {
    if (window.CKEDITOR) {
       clearInterval(waitCKEDITOR);
       //CKEDITOR.replace(...);
    }
}, 100/*milli*/);

回答by Pekka

I've looked through the API docs, but could only find the loaded event.

我查看了 API 文档,但只能找到加载的事件。

I don't know whether there exists a specific property for this - there might! - but you could use the loaded event to set a global flag. It's not really nice but would do the job.

我不知道是否存在特定的属性 - 可能有!- 但您可以使用加载的事件来设置全局标志。这不是很好,但可以完成这项工作。

// At the top of the script
CKEDitor_loaded = false;

// then later
CKEDITOR.on('loaded', function(){ CKEditor_loaded = true; });

Instead of a global variable, you could also consider setting something inside CKEDITOR:

除了全局变量,您还可以考虑在里面设置一些东西CKEDITOR

CKEDITOR.flag_loaded = true;

This would be a bit cleaner.

这样会干净一些。

回答by Convictions

I know this is a very old post, but in my research it kept coming up. I am dynamically loading the CKEditorthrough jQuery. I didn't want to load it multiple times since things start happening, as you found out.

我知道这是一篇很老的帖子,但在我的研究中它不断出现。我正在动态加载CKEditorthrough jQuery。正如您发现的那样,自从事情开始发生以来,我不想多次加载它。

Simple solution:

简单的解决方案:

if (!window.CKEDITOR) {
    // (not loaded yet, your code to load it)
}

回答by Jonathan Parker

Hope this helps someone.

希望这可以帮助某人。

I also load a page snippet with CKEDITOR functionality via AJAX and as such I have experienced many of the problems outlined in this question. This is my solution:

我还通过 AJAX 加载了一个带有 CKEDITOR 功能的页面片段,因此我遇到了这个问题中列出的许多问题。这是我的解决方案:

function setCk(id){
if(window.CKEDITOR){
    var _instId = CKEDITOR.instances[id];
    if(_instId == undefined){
        CKEDITOR.inline(id);
    }else{
        CKEDITOR.instances[id].destroy();
        CKEDITOR.inline(id);
    }
}

}

}

On each AJAX response for this snippet I inject a script element into the head with a call to setCk(textareaId). The trick being to destroy any previous CKEDITOR instances for the target ID & re-initialise CKEDITOR after each AJAX snippet load.

在此代码段的每个 AJAX 响应中,我通过调用 setCk(textareaId) 将脚本元素注入头部。诀窍是在每个 AJAX 片段加载后销毁目标 ID 的任何先前 CKEDITOR 实例并重新初始化 CKEDITOR。

回答by devendra singh

//creating instance of ck-editor
var yourInstance = CKEDITOR.instances.yourContainer;
//check instance of your ck-editor 
if(yourInstance){
      //destroy instance
      yourInstance .destroy(true); 
}
// create instance again
CKEDITOR.replace( 'yourContainer' );

回答by Fab

If instance is not ready, the text set would be discarded

如果实例未准备好,则文本集将被丢弃

On initialization of the CkEditor (version 4 here), you should never set any data before the editor is ready to handle it.

在 CkEditor(此处为第 4 版)的初始化过程中,您不应在编辑器准备好处理数据之前设置任何数据。

// Initialize this._editor with replace

if (this._editor.status !== "ready") {
    this._editor.on("instanceReady",
        event => {
            event.editor.setData(data);
        });
} else {
    this._editor.setData(data);
}