1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3 Copyright (c) 2003-2011, CKSource - Frederico Knabben. All rights reserved.
4 For licensing, see LICENSE.html or http://ckeditor.com/license
6 <html xmlns="http://www.w3.org/1999/xhtml">
8 <title>API Usage — CKEditor Sample</title>
9 <meta content="text/html; charset=utf-8" http-equiv="content-type" />
10 <script type="text/javascript" src="../ckeditor.js"></script>
11 <script src="sample.js" type="text/javascript"></script>
12 <link href="sample.css" rel="stylesheet" type="text/css" />
13 <script type="text/javascript">
16 // The instanceReady event is fired, when an instance of CKEditor has finished
17 // its initialization.
18 CKEDITOR.on( 'instanceReady', function( ev )
20 // Show the editor name and description in the browser status bar.
21 document.getElementById( 'eMessage' ).innerHTML = '<p>Instance <code>' + ev.editor.name + '<\/code> loaded.<\/p>';
23 // Show this sample buttons.
24 document.getElementById( 'eButtons' ).style.display = 'block';
29 // Get the editor instance that we want to interact with.
30 var oEditor = CKEDITOR.instances.editor1;
31 var value = document.getElementById( 'htmlArea' ).value;
33 // Check the active editing mode.
34 if ( oEditor.mode == 'wysiwyg' )
37 // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#insertHtml
38 oEditor.insertHtml( value );
41 alert( 'You must be in WYSIWYG mode!' );
46 // Get the editor instance that we want to interact with.
47 var oEditor = CKEDITOR.instances.editor1;
48 var value = document.getElementById( 'txtArea' ).value;
50 // Check the active editing mode.
51 if ( oEditor.mode == 'wysiwyg' )
53 // Insert as plain text.
54 // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#insertText
55 oEditor.insertText( value );
58 alert( 'You must be in WYSIWYG mode!' );
61 function SetContents()
63 // Get the editor instance that we want to interact with.
64 var oEditor = CKEDITOR.instances.editor1;
65 var value = document.getElementById( 'htmlArea' ).value;
67 // Set editor contents (replace current contents).
68 // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#setData
69 oEditor.setData( value );
72 function GetContents()
74 // Get the editor instance that you want to interact with.
75 var oEditor = CKEDITOR.instances.editor1;
77 // Get editor contents
78 // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#getData
79 alert( oEditor.getData() );
82 function ExecuteCommand( commandName )
84 // Get the editor instance that we want to interact with.
85 var oEditor = CKEDITOR.instances.editor1;
87 // Check the active editing mode.
88 if ( oEditor.mode == 'wysiwyg' )
90 // Execute the command.
91 // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#execCommand
92 oEditor.execCommand( commandName );
95 alert( 'You must be in WYSIWYG mode!' );
100 // Get the editor instance that we want to interact with.
101 var oEditor = CKEDITOR.instances.editor1;
102 // Checks whether the current editor contents present changes when compared
103 // to the contents loaded into the editor at startup
104 // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#checkDirty
105 alert( oEditor.checkDirty() );
108 function ResetDirty()
110 // Get the editor instance that we want to interact with.
111 var oEditor = CKEDITOR.instances.editor1;
112 // Resets the "dirty state" of the editor (see CheckDirty())
113 // http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#resetDirty
114 oEditor.resetDirty();
115 alert( 'The "IsDirty" status has been reset' );
124 CKEditor Sample — Using CKEditor JavaScript API
126 <div class="description">
128 This sample shows how to use the
129 <a class="samples" href="http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html">CKEditor JavaScript API</a>
130 to interact with the editor at runtime.
133 For details on how to create this setup check the source code of this sample page.
137 <!-- This <div> holds alert messages to be display in the sample page. -->
141 <strong>CKEditor requires JavaScript to run</strong>. In a browser with no JavaScript
142 support, like yours, you should still see the contents (HTML data) and you should
143 be able to edit it normally, without a rich editor interface.
147 <form action="sample_posteddata.php" method="post">
148 <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>
150 <script type="text/javascript">
152 // Replace the <textarea id="editor1"> with an CKEditor instance.
153 var editor = CKEDITOR.replace( 'editor1' );
159 <div id="eButtons" style="display: none">
160 <input onclick="InsertHTML();" type="button" value="Insert HTML" />
161 <input onclick="SetContents();" type="button" value="Set Editor Contents" />
162 <input onclick="GetContents();" type="button" value="Get Editor Contents (XHTML)" />
164 <textarea cols="100" id="htmlArea" rows="3"><h2>Test</h2><p>This is some <a href="/Test1.html">sample</a> HTML code.</p></textarea>
167 <input onclick="InsertText();" type="button" value="Insert Text" />
169 <textarea cols="100" id="txtArea" rows="3"> First line with some leading whitespaces.
171 Second line of text preceded by two line breaks.</textarea>
173 <input onclick="ExecuteCommand('bold');" type="button" value="Execute "bold" Command" />
174 <input onclick="ExecuteCommand('link');" type="button" value="Execute "link" Command" />
177 <input onclick="CheckDirty();" type="button" value="checkDirty()" />
178 <input onclick="ResetDirty();" type="button" value="resetDirty()" />
184 CKEditor - The text editor for the Internet - <a class="samples" href="http://ckeditor.com/">http://ckeditor.com</a>
187 Copyright © 2003-2011, <a class="samples" href="http://cksource.com/">CKSource</a> - Frederico
188 Knabben. All rights reserved.