3 Copyright (c) 2003-2016, 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="warning deprecated">
128 This sample is not maintained anymore. Check out its <a href="http://sdk.ckeditor.com/samples/api.html">brand new version in CKEditor SDK</a>.
130 <div class="description">
132 This sample shows how to use the
133 <a class="samples" href="http://docs.ckeditor.com/#!/api/CKEDITOR.editor">CKEditor JavaScript API</a>
134 to interact with the editor at runtime.
137 For details on how to create this setup check the source code of this sample page.
141 <!-- This <div> holds alert messages to be display in the sample page. -->
145 <strong>CKEditor requires JavaScript to run</strong>. In a browser with no JavaScript
146 support, like yours, you should still see the contents (HTML data) and you should
147 be able to edit it normally, without a rich editor interface.
151 <form action="../../../samples/sample_posteddata.php" method="post">
152 <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>
155 // Replace the <textarea id="editor1"> with an CKEditor instance.
156 CKEDITOR.replace( 'editor1', {
161 // Check for availability of corresponding plugins.
162 pluginsLoaded: function( evt ) {
163 var doc = CKEDITOR.document, ed = evt.editor;
164 if ( !ed.getCommand( 'bold' ) )
165 doc.getById( 'exec-bold' ).hide();
166 if ( !ed.getCommand( 'link' ) )
167 doc.getById( 'exec-link' ).hide();
176 <div id="eButtons" style="display: none">
177 <input id="exec-bold" onclick="ExecuteCommand('bold');" type="button" value="Execute "bold" Command">
178 <input id="exec-link" onclick="ExecuteCommand('link');" type="button" value="Execute "link" Command">
179 <input onclick="Focus();" type="button" value="Focus">
181 <input onclick="InsertHTML();" type="button" value="Insert HTML">
182 <input onclick="SetContents();" type="button" value="Set Editor Contents">
183 <input onclick="GetContents();" type="button" value="Get Editor Contents (HTML)">
185 <textarea cols="100" id="htmlArea" rows="3"><h2>Test</h2><p>This is some <a href="/Test1.html">sample</a> HTML code.</p></textarea>
188 <input onclick="InsertText();" type="button" value="Insert Text">
190 <textarea cols="100" id="txtArea" rows="3"> First line with some leading whitespaces.
192 Second line of text preceded by two line breaks.</textarea>
195 <input onclick="CheckDirty();" type="button" value="checkDirty()">
196 <input onclick="ResetDirty();" type="button" value="resetDirty()">
202 CKEditor - The text editor for the Internet - <a class="samples" href="http://ckeditor.com/">http://ckeditor.com</a>
205 Copyright © 2003-2016, <a class="samples" href="http://cksource.com/">CKSource</a> - Frederico
206 Knabben. All rights reserved.