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>Adding Event Handlers — CKEditor Sample</title>
9 <meta content="text/html; charset=utf-8" http-equiv="content-type"/>
10 <link href="../sample.css" rel="stylesheet" type="text/css"/>
14 CKEditor Sample — Adding Event Handlers
16 <div class="description">
18 This sample shows how to add event handlers to CKEditor with PHP.
21 A snippet of the configuration code can be seen below; check the source code of this page for
24 <pre class="samples"><?php
25 // Include the CKEditor class.
26 include("ckeditor/ckeditor.php");
28 // Create a class instance.
29 $CKEditor = new CKEditor();
31 // Path to the CKEditor directory.
32 $CKEditor->basePath = '/ckeditor/';
34 // The initial value to be displayed in the editor.
35 $initialValue = 'This is some sample text.';
37 // Add event handler, <em>instanceReady</em> is fired when editor is loaded.
38 $CKEditor-><strong>addEventHandler</strong>('instanceReady', 'function (evt) {
39 alert("Loaded editor: " + evt.editor.name);
42 // Create an editor instance.
43 $CKEditor->editor("editor1", $initialValue);
46 <!-- This <div> holds alert messages to be display in the sample page. -->
50 <strong>CKEditor requires JavaScript to run</strong>. In a browser with no JavaScript
51 support, like yours, you should still see the contents (HTML data) and you should
52 be able to edit it normally, without a rich editor interface.
56 <form action="../sample_posteddata.php" method="post">
57 <label>Editor 1:</label>
61 * Adds a global event, will hide the "Target" tab in the "Link" dialog window in all instances.
63 function CKEditorHideLinkTargetTab(&$CKEditor) {
65 $function = 'function (ev) {
66 // Take the dialog window name and its definition from the event data.
67 var dialogName = ev.data.name;
68 var dialogDefinition = ev.data.definition;
70 // Check if the definition comes from the "Link" dialog window.
71 if ( dialogName == "link" )
72 dialogDefinition.removeContents("target")
75 $CKEditor->addGlobalEventHandler('dialogDefinition', $function);
79 * Adds a global event, will notify about an open dialog window.
81 function CKEditorNotifyAboutOpenedDialog(&$CKEditor) {
82 $function = 'function (evt) {
83 alert("Loading a dialog window: " + evt.data.name);
86 $CKEditor->addGlobalEventHandler('dialogDefinition', $function);
89 // Include the CKEditor class.
90 include("../../ckeditor.php");
92 // Create a class instance.
93 $CKEditor = new CKEditor();
95 // Set a configuration option for all editors.
96 $CKEditor->config['width'] = 750;
98 // Path to the CKEditor directory, ideally use an absolute path instead of a relative dir.
99 // $CKEditor->basePath = '/ckeditor/'
100 // If not set, CKEditor will try to detect the correct path.
101 $CKEditor->basePath = '../../';
103 // The initial value to be displayed in the editor.
104 $initialValue = '<p>This is some <strong>sample text</strong>. You are using <a href="http://ckeditor.com/">CKEditor</a>.</p>';
106 // Event that will be handled only by the first editor.
107 $CKEditor->addEventHandler('instanceReady', 'function (evt) {
108 alert("Loaded editor: " + evt.editor.name);
111 // Create the first instance.
112 $CKEditor->editor("editor1", $initialValue);
114 // Clear event handlers. Instances that will be created later will not have
115 // the 'instanceReady' listener defined a couple of lines above.
116 $CKEditor->clearEventHandlers();
119 <label>Editor 2:</label>
121 // Configuration that will only be used by the second editor.
122 $config['width'] = '600';
123 $config['toolbar'] = 'Basic';
125 // Add some global event handlers (for all editors).
126 CKEditorHideLinkTargetTab($CKEditor);
127 CKEditorNotifyAboutOpenedDialog($CKEditor);
129 // Event that will only be handled by the second editor.
130 // Instead of calling addEventHandler(), events may be passed as an argument.
131 $events['instanceReady'] = 'function (evt) {
132 alert("Loaded second editor: " + evt.editor.name);
135 // Create the second instance.
136 $CKEditor->editor("editor2", $initialValue, $config, $events);
139 <input type="submit" value="Submit"/>
145 CKEditor - The text editor for the Internet - <a class="samples" href="http://ckeditor.com/">http://ckeditor.com</a>
148 Copyright © 2003-2011, <a class="samples" href="http://cksource.com/">CKSource</a> - Frederico
149 Knabben. All rights reserved.