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