3 Copyright (c) 2003-2014, CKSource - Frederico Knabben. All rights reserved.
4 For licensing, see LICENSE.md or http://ckeditor.com/license
9 <title>API Usage
— CKEditor Sample
</title>
10 <script src=
"../ckeditor.js"></script>
11 <link href=
"sample.css" rel=
"stylesheet">
14 // The instanceReady event is fired, when an instance of CKEditor has finished
15 // its initialization.
16 CKEDITOR.on( 'instanceReady', function( ev ) {
17 // Show the editor name and description in the browser status bar.
18 document.getElementById( 'eMessage' ).innerHTML = 'Instance
<code>' + ev.editor.name + '<\/code
> loaded.';
20 // Show this sample buttons.
21 document.getElementById( 'eButtons' ).style.display = 'block';
24 function InsertHTML() {
25 // Get the editor instance that we want to interact with.
26 var editor = CKEDITOR.instances.editor1;
27 var value = document.getElementById( 'htmlArea' ).value;
29 // Check the active editing mode.
30 if ( editor.mode == 'wysiwyg' )
33 // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-insertHtml
34 editor.insertHtml( value );
37 alert( 'You must be in WYSIWYG mode!' );
40 function InsertText() {
41 // Get the editor instance that we want to interact with.
42 var editor = CKEDITOR.instances.editor1;
43 var value = document.getElementById( 'txtArea' ).value;
45 // Check the active editing mode.
46 if ( editor.mode == 'wysiwyg' )
48 // Insert as plain text.
49 // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-insertText
50 editor.insertText( value );
53 alert( 'You must be in WYSIWYG mode!' );
56 function SetContents() {
57 // Get the editor instance that we want to interact with.
58 var editor = CKEDITOR.instances.editor1;
59 var value = document.getElementById( 'htmlArea' ).value;
61 // Set editor contents (replace current contents).
62 // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-setData
63 editor.setData( value );
66 function GetContents() {
67 // Get the editor instance that you want to interact with.
68 var editor = CKEDITOR.instances.editor1;
70 // Get editor contents
71 // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-getData
72 alert( editor.getData() );
75 function ExecuteCommand( commandName ) {
76 // Get the editor instance that we want to interact with.
77 var editor = CKEDITOR.instances.editor1;
79 // Check the active editing mode.
80 if ( editor.mode == 'wysiwyg' )
82 // Execute the command.
83 // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-execCommand
84 editor.execCommand( commandName );
87 alert( 'You must be in WYSIWYG mode!' );
90 function CheckDirty() {
91 // Get the editor instance that we want to interact with.
92 var editor = CKEDITOR.instances.editor1;
93 // Checks whether the current editor contents present changes when compared
94 // to the contents loaded into the editor at startup
95 // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-checkDirty
96 alert( editor.checkDirty() );
99 function ResetDirty() {
100 // Get the editor instance that we want to interact with.
101 var editor = CKEDITOR.instances.editor1;
102 // Resets the
"dirty state" of the editor (see CheckDirty())
103 // http://docs.ckeditor.com/#!/api/CKEDITOR.editor-method-resetDirty
105 alert( 'The
"IsDirty" status has been reset' );
109 CKEDITOR.instances.editor1.focus();
113 document.getElementById( 'eMessage' ).innerHTML = '
<b>' + this.name + ' is focused
</b>';
117 document.getElementById( 'eMessage' ).innerHTML = this.name + ' lost focus';
125 <a href=
"index.html">CKEditor Samples
</a> » Using CKEditor JavaScript API
127 <div class=
"description">
129 This sample shows how to use the
130 <a class=
"samples" href=
"http://docs.ckeditor.com/#!/api/CKEDITOR.editor">CKEditor JavaScript API
</a>
131 to interact with the editor at runtime.
134 For details on how to create this setup check the source code of this sample page.
138 <!-- This <div> holds alert messages to be display in the sample page. -->
142 <strong>CKEditor requires JavaScript to run
</strong>. In a browser with no JavaScript
143 support, like yours, you should still see the contents (HTML data) and you should
144 be able to edit it normally, without a rich editor interface.
148 <form action=
"../../../samples/sample_posteddata.php" method=
"post">
149 <textarea cols=
"100" id=
"editor1" name=
"editor1" rows=
"10"><p
>This is some
<strong
>sample text
</strong
>. You are using
<a
href=
"http://ckeditor.com/">CKEditor
</a
>.
</p
></textarea>
152 // Replace the
<textarea id=
"editor1"> with an CKEditor instance.
153 CKEDITOR.replace( 'editor1', {
158 // Check for availability of corresponding plugins.
159 pluginsLoaded: function( evt ) {
160 var doc = CKEDITOR.document, ed = evt.editor;
161 if ( !ed.getCommand( 'bold' ) )
162 doc.getById( 'exec-bold' ).hide();
163 if ( !ed.getCommand( 'link' ) )
164 doc.getById( 'exec-link' ).hide();
173 <div id=
"eButtons" style=
"display: none">
174 <input id=
"exec-bold" onclick=
"ExecuteCommand('bold');" type=
"button" value=
"Execute "bold" Command">
175 <input id=
"exec-link" onclick=
"ExecuteCommand('link');" type=
"button" value=
"Execute "link" Command">
176 <input onclick=
"Focus();" type=
"button" value=
"Focus">
178 <input onclick=
"InsertHTML();" type=
"button" value=
"Insert HTML">
179 <input onclick=
"SetContents();" type=
"button" value=
"Set Editor Contents">
180 <input onclick=
"GetContents();" type=
"button" value=
"Get Editor Contents (HTML)">
182 <textarea cols=
"100" id=
"htmlArea" rows=
"3"><h2
>Test
</h2
><p
>This is some
<a
href=
"/Test1.html">sample
</a
> HTML code.
</p
></textarea>
185 <input onclick=
"InsertText();" type=
"button" value=
"Insert Text">
187 <textarea cols=
"100" id=
"txtArea" rows=
"3"> First line with some leading whitespaces.
189 Second line of text preceded by two line breaks.
</textarea>
192 <input onclick=
"CheckDirty();" type=
"button" value=
"checkDirty()">
193 <input onclick=
"ResetDirty();" type=
"button" value=
"resetDirty()">
199 CKEditor - The text editor for the Internet -
<a class=
"samples" href=
"http://ckeditor.com/">http://ckeditor.com
</a>
202 Copyright
© 2003-
2014,
<a class=
"samples" href=
"http://cksource.com/">CKSource
</a> - Frederico
203 Knabben. All rights reserved.