Integration"

This website contains links to software which is either no longer maintained or will be supported only until the end of 2019 (CKFinder 2). For the latest documentation about current CKSource projects, including software like CKEditor 4/CKEditor 5, CKFinder 3, Cloud Services, Letters, Accessibility Checker, please visit the new documentation website.

If you look for an information about very old versions of CKEditor, FCKeditor and CKFinder check also the CKEditor forum, which was closed in 2015. If not, please head to StackOverflow for support.

(JavaScript API)
(Complete Sample: HTML encoding added)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
 +
__TOC__
 
There are several ways to integrate CKEditor into your pages. This page presents the most common way to achieve it.  
 
There are several ways to integrate CKEditor into your pages. This page presents the most common way to achieve it.  
  
== Step 1: Loading CKEditor ==
+
== Step 1: Loading CKEditor ==
 +
CKEditor is a JavaScript application. To load it, you need to include a single file reference in your page. If you have [[CKEditor 3.x/Developers Guide/Installation|installed]] CKEditor in the <code>ckeditor</code> directory for the root of your website, you need to insert the following code fragment into the <code><head></code> section of your page:
  
CKEditor is a JavaScript application. To load it, you just need to include a single file reference at your page. Supposing that you have [[CKEditor 3.x/Developers Guide/Installation|installed]] CKEditor at the "ckeditor" directory at the root of your web site, here you have an example:
+
<source><head>
<pre>&lt;head&gt;
 
 
...
 
...
&lt;script type="text/javascript" src="/ckeditor/ckeditor.js"&gt;&lt;/script&gt;
+
<script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
&lt;/head&gt;</pre>  
+
</head>
With the above file loaded, the CKEditor JavaScript API is ready to be used.  
+
</source>  
 +
When the above file is loaded, the CKEditor JavaScript API is ready to be used.  
  
== Step 2: Creating an Editor Instance  ==
+
<note>When adding CKEditor to your web pages, use the original file name (<code>ckeditor.js</code>). If you want to use a different file name, or even merge the CKEditor script into another JavaScript file, refer to the [[CKEditor 3.x/Developers Guide/Specifying the Editor Path|Specifying the Editor Path]] section of the Developer's Guide first.</note>
  
CKEditor works just like a textarea in your page. While the editor offers a user interface to easily write, format and work with rich text, the same thing could be achieved, not that easily, with a textarea, requiring the user to type HTML code inside of it.
 
  
But, as a matter of fact, CKEditor uses a textarea to transfer its data to the server. The textarea is invisible to the end user. So, to create and editor instance you must, ironically, create a textarea first:
+
== Step 2: Creating an Editor Instance ==
<pre>&lt;textarea name="editor1"&gt;&amp;lt;p&amp;gt;Initial value.&amp;lt;/p&amp;gt;&lt;/textarea&gt;</pre>  
+
CKEditor works just like a textarea element in your page. The editor offers a user interface to easily write, format, and work with rich text, but the same thing could be achieved (though not that easily) with a <code><textarea></code> element, requiring the user to type HTML code inside.  
Note that, if you want to load data into the editor, from a database for example, just put that data inside the textarea, just like the above example.  
 
  
In this example, we have named our textarea "editor1". This name can be used in the server side later, when receiving the posted data.  
+
As a matter of fact, CKEditor uses the textarea to transfer its data to the server. The textarea element is invisible to the end user. In order to create an editor instance, you must first add a <code><textarea></code> element to the source code of your HTML page:
 +
<source><textarea id="editor1" name="editor1">&lt;p&gt;Initial value.&lt;/p&gt;</textarea></source>
  
Now, using the CKEditor JavaScript API, we "replace" the plain textarea with an editor instance. A single JavaScript call is needed for that:  
+
Note that if you want to load data into the editor, for example from a database, you need to put that data inside the <code><textarea></code> element, just like the HTML-encoded <code>&lt;p&gt;</code> element in the example above. In this case the textarea element was named <code>editor1</code>. This name can be used in the server-side code later, when receiving the posted data.
<pre>&lt;script type="text/javascript"&gt;
+
 
 +
After the <code>textarea</code> element is inserted, we can use the [http://docs.cksource.com/ckeditor_api/ CKEditor JavaScript API] to replace this HTML element with an editor instance. A single JavaScript <code>[http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.html#.replace replace]</code> call is needed for that:  
 +
<source lang="javascript"><script type="text/javascript">
 
CKEDITOR.replace( 'editor1' );
 
CKEDITOR.replace( 'editor1' );
&lt;/script&gt;</pre>  
+
</script></source>
This script block must be included at any point "after" the &lt;textarea&gt; tag in the page. You can also call the replace function inside &lt;head&gt;, but you need to listen for the "window.onload" event in such case:  
+
<pre>&lt;script type="text/javascript"&gt;
+
This script block must be included at any point '''after''' the <code><textarea></code> tag in the source code of the page. You can also call the <code>replace</code> function inside the <code><head></code> section, but in this case you will need to listen for the <code>window.onload</code> event:  
 +
 
 +
<source lang="javascript"><script type="text/javascript">
 
window.onload = function()
 
window.onload = function()
 
{
 
{
 
CKEDITOR.replace( 'editor1' );
 
CKEDITOR.replace( 'editor1' );
 
};
 
};
&lt;/script&gt;</pre>  
+
</script></source>
== Step 3: Saving the Editor Data  ==
 
  
As previously described, the editor works just like a &lt;textarea&gt; field. So, when submitting a form containing an editor instance, its data will be simply posted, using the textarea name as the key to retrieve it.  
+
== Step 3: Saving the Editor Data ==
 +
As stated above, the editor works just like a <code><textarea></code> field. This means that when submitting a form containing an editor instance, its data will be simply posted, using the <code><textarea></code> element name as the key to retrieve it.  
  
For example, following the above example, in PHP we would do something like this:  
+
For example, following the above example, we could create the following PHP code:  
<pre>&lt;?php
+
<source lang="php"><?php
$editor_data = $_POST[ 'editor1' ];
+
$editor_data = $_POST[ 'editor1' ];
?&gt;</pre>  
+
?></source>  
=== Client Side Data Handling  ===
 
  
Some applications (like Ajax applications) need to handle all data at the client side, sending it to the server by its own ways. In such case, it's enough to use the CKEditor API to easily retrieve an editor instance data. For example:  
+
=== Client-Side Data Handling ===
<pre>&lt;script type="text/javascript"&gt;
+
Some applications (like those based on Ajax) need to handle all data on the client side, sending it to the server using their specific methods. If this is the case, it is enough to use the CKEditor API to easily retrieve the editor instance data. In order to do this, you can use the [http://docs.cksource.com/ckeditor_api/symbols/CKEDITOR.editor.html#getData getData] method:
 +
<source language="javascript"><script type="text/javascript">
 
var editor_data = CKEDITOR.instances.editor1.getData();
 
var editor_data = CKEDITOR.instances.editor1.getData();
&lt;/script&gt;</pre>  
+
</script></source>
== Complete Sample ==
+
 
<pre>&lt;html&gt;
+
== Complete Sample ==
&lt;head&gt;
+
To insert a CKEditor instance, you can use the following sample that creates a basic page containing a form with a <code>textarea</code> element that is replaced with CKEditor.
&lt;title&gt;Sample - CKEditor&lt;/title&gt;
+
<source>
&lt;script type="text/javascript" src="/ckeditor/ckeditor.js"&gt;&lt;/script&gt;
+
<html>
&lt;/head&gt;
+
<head>
&lt;body&gt;
+
<title>Sample CKEditor Site</title>
&lt;form method="post"&gt;
+
<script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
&lt;p&gt;
+
</head>
My Editor:&lt;br /&gt;
+
<body>
&lt;textarea name="editor1"&gt;&amp;lt;p&amp;gt;Initial value.&amp;lt;/p&amp;gt;&lt;/textarea&gt;
+
<form method="post">
&lt;script type="text/javascript"&gt;
+
<p>
 +
My Editor:<br />
 +
<textarea id="editor1" name="editor1">&lt;p&gt;Initial value.&lt;/p&gt;</textarea>
 +
<script type="text/javascript">
 
CKEDITOR.replace( 'editor1' );
 
CKEDITOR.replace( 'editor1' );
&lt;/script&gt;
+
</script>
&lt;/p&gt;
+
</p>
&lt;p&gt;
+
<p>
&lt;input type="submit" /&gt;
+
<input type="submit" />
&lt;/p&gt;
+
</p>
&lt;/form&gt;
+
</form>
&lt;/body&gt;
+
</body>
&lt;/html&gt;
+
</html>
</pre>
+
</source>

Latest revision as of 14:54, 10 March 2011

There are several ways to integrate CKEditor into your pages. This page presents the most common way to achieve it.

Step 1: Loading CKEditor

CKEditor is a JavaScript application. To load it, you need to include a single file reference in your page. If you have installed CKEditor in the ckeditor directory for the root of your website, you need to insert the following code fragment into the <head> section of your page:

<head>
	...
	<script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
</head>

When the above file is loaded, the CKEditor JavaScript API is ready to be used.

important note
When adding CKEditor to your web pages, use the original file name (ckeditor.js). If you want to use a different file name, or even merge the CKEditor script into another JavaScript file, refer to the Specifying the Editor Path section of the Developer's Guide first.


Step 2: Creating an Editor Instance

CKEditor works just like a textarea element in your page. The editor offers a user interface to easily write, format, and work with rich text, but the same thing could be achieved (though not that easily) with a <textarea> element, requiring the user to type HTML code inside.

As a matter of fact, CKEditor uses the textarea to transfer its data to the server. The textarea element is invisible to the end user. In order to create an editor instance, you must first add a <textarea> element to the source code of your HTML page:

<textarea id="editor1" name="editor1">&lt;p&gt;Initial value.&lt;/p&gt;</textarea>

Note that if you want to load data into the editor, for example from a database, you need to put that data inside the <textarea> element, just like the HTML-encoded <p> element in the example above. In this case the textarea element was named editor1. This name can be used in the server-side code later, when receiving the posted data.

After the textarea element is inserted, we can use the CKEditor JavaScript API to replace this HTML element with an editor instance. A single JavaScript replace call is needed for that:

<script type="text/javascript">
	CKEDITOR.replace( 'editor1' );
</script>

This script block must be included at any point after the <textarea> tag in the source code of the page. You can also call the replace function inside the <head> section, but in this case you will need to listen for the window.onload event:

<script type="text/javascript">
	window.onload = function()
	{
		CKEDITOR.replace( 'editor1' );
	};
</script>

Step 3: Saving the Editor Data

As stated above, the editor works just like a <textarea> field. This means that when submitting a form containing an editor instance, its data will be simply posted, using the <textarea> element name as the key to retrieve it.

For example, following the above example, we could create the following PHP code:

<?php
	$editor_data = $_POST[ 'editor1' ];
?>

Client-Side Data Handling

Some applications (like those based on Ajax) need to handle all data on the client side, sending it to the server using their specific methods. If this is the case, it is enough to use the CKEditor API to easily retrieve the editor instance data. In order to do this, you can use the getData method:

<script type="text/javascript">
	var editor_data = CKEDITOR.instances.editor1.getData();
</script>

Complete Sample

To insert a CKEditor instance, you can use the following sample that creates a basic page containing a form with a textarea element that is replaced with CKEditor.

<html>
<head>
	<title>Sample CKEditor Site</title>
	<script type="text/javascript" src="/ckeditor/ckeditor.js"></script>
</head>
<body>
	<form method="post">
		<p>
			My Editor:<br />
			<textarea id="editor1" name="editor1">&lt;p&gt;Initial value.&lt;/p&gt;</textarea>
			<script type="text/javascript">
				CKEDITOR.replace( 'editor1' );
			</script>
		</p>
		<p>
			<input type="submit" />
		</p>
	</form>
</body>
</html>
This page was last edited on 10 March 2011, at 14:54.