最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to load data into ckeditor onload - Stack Overflow

programmeradmin3浏览0评论

Is it possible to load data into the ckeditor without using JQuery? I would like to use an inline script tag for this if possible. All of the examples I can find deal with Jquery and that isn't optimal in my case.

What I have so far is the example I found on CKs site:

CKEDITOR.instances.editor1.setData( '<p>Some other editor data.</p>', function()
    {
        this.checkDirty();  // true
    });

I've tried to use this with an inline script tag but it crashes the editor. If I use this in JQuery, it will work as expected.

Is it possible to load data into the ckeditor without using JQuery? I would like to use an inline script tag for this if possible. All of the examples I can find deal with Jquery and that isn't optimal in my case.

What I have so far is the example I found on CKs site:

CKEDITOR.instances.editor1.setData( '<p>Some other editor data.</p>', function()
    {
        this.checkDirty();  // true
    });

I've tried to use this with an inline script tag but it crashes the editor. If I use this in JQuery, it will work as expected.

Share Improve this question edited Jan 4, 2013 at 3:23 NaN asked Jan 4, 2013 at 3:15 NaNNaN 1,3062 gold badges17 silver badges31 bronze badges 2
  • can you show some code so that it will easy for us respond back. – Philemon philip Kunjumon Commented Jan 4, 2013 at 3:21
  • Philemon, I posted all that I have right now which isn't much – NaN Commented Jan 4, 2013 at 3:24
Add a comment  | 

4 Answers 4

Reset to default 17

Here is an example using CKEDITOR API without using jQuery. Please observe the call to replace function which initializes the "editor1" instance of the CKEDITOR with the textarea content. I think that would be the easiest way to populate the content onload, but if that will not suffice, you can see I've used the code in your question to populate the CKEDITOR with "Some other editor data."

HTML:

<textarea id="editor1">
    Hello, world!
</textarea>​

Javascript:

CKEDITOR.replace( 'editor1', {
    toolbar: 'Basic',
    uiColor: '#9AB8F3'
});
CKEDITOR.instances.editor1.setData( '<p>Some other editor data.</p>', function()
{
    this.checkDirty();  // true
});

http://jsfiddle.net/cDzqp/

I know this question is old but I've just figured out my solution and I think is cleaner so I'm adding my two cents here for whoever might have the same problem in the future.

CKEditor automatically sets the content of the new textarea using what is originally within the old, replaced one. In other words, it's sufficient to change the content of the textarea and only then call the replace command. This would be the code:

document.getElementById("editor1").value = "<p>Some other editor data.</p>";
CKEDITOR.replace("editor1");

Done :)

If someone is looking for solution to append content to existing content in CKEditor this will help. - I am using CKEditor 4.15.0

CKEDITOR.replace( 'editor1', {
toolbar: 'Basic',
uiColor: '#9AB8F3'});

CKEDITOR.instances.editor1.setData( CKEDITOR.instances.editor1.getData + 'New Content Here', function()
{
    this.checkDirty();  // true
});

modified as per my requirement, based on answer by Patrick Jones

Thanks

Transforms input data into HTML to be loaded into the editor. While the editor is able to handle non-HTML data (like BBCode), it can only handle HTML data at runtime. The role of the data processor is to transform the input data into HTML through this function.

See source

// Tranforming BBCode data, with a custom BBCode data processor available.
var data = 'This is [b]an example[/b].';
var html = editor.dataProcessor.toHtml( data ); // '<p>This is <b>an example</b>.</p>'
发布评论

评论列表(0)

  1. 暂无评论