JavaScript API"

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.

Line 1: Line 1:
== FCKeditor JavaScript API ==
+
== FCKeditor JavaScript API ==
  
FCKeditor offers a complete JavaScript API so you can interact with it once the editor is loaded and running.
+
FCKeditor offers a complete JavaScript API so you can interact with it once the editor is loaded and running.  
  
Once loaded, the editor registers a global object called '''FCKeditorAPI'''. This object offers the entry point to interact with any editor instance placed in a page (you can have more than one).
+
Once loaded, the editor registers a global object called '''FCKeditorAPI'''. This object offers the entry point to interact with any editor instance placed in a page (you can have more than one).  
  
'''NOTE''': The '''FCKeditorAPI''' object will not be available during the page load. You need to wait for the editor to be loaded to use it. If you need to interact with the editor right after is has been loaded, use the "FCKeditor_OnComplete" function (see [[FCKeditor 2.x/Developers Guide/JavaScript API#Events|Events]]).
+
'''NOTE''': The '''FCKeditorAPI''' object will not be available during the page load. You need to wait for the editor to be loaded to use it. If you need to interact with the editor right after is has been loaded, use the "FCKeditor_OnComplete" function (see [[FCKeditor 2.x/Developers Guide/JavaScript API#Events|Events]]).  
  
== Retrieving an Editor Instance ==
+
== Retrieving an Editor Instance ==
  
* '''From out of an external script''' <br>When placing the editor in the page, you give it an "instance name". To retrieve it, you must simply call the '''FCKeditorAPI.GetInstance''' method.<br>Example:<pre>var oEditor = FCKeditorAPI.GetInstance('InstanceName')&nbsp;;
+
*'''From out of an external script''' <br>When placing the editor in the page, you give it an "instance name". To retrieve it, you must simply call the '''FCKeditorAPI.GetInstance''' method.<br>Example:<pre>var oEditor = FCKeditorAPI.GetInstance('InstanceName')&nbsp;;
 
</pre>
 
</pre>
  
* '''From out of a dialog of the editor'''<br>Call the InnerDialogLoaded to get the FCKObject.<br>Example:<pre>var oEditor = window.parent.InnerDialogLoaded().FCK&nbsp;;
+
*'''From out of a dialog of the editor'''<br>Call the InnerDialogLoaded to get the FCKObject.<br>Example:<pre>var oEditor = window.parent.InnerDialogLoaded().FCK&nbsp;;
 
</pre>
 
</pre>
  
Both methods return the main FCKeditor object that gives the necessary bridge to interact with it. These are the most useful properties and methods of this object:
+
Both methods return the main FCKeditor object that gives the necessary bridge to interact with it. These are the most useful properties and methods of this object:  
  
Properties:
+
Properties:  
  
* '''Name = ( string )''' - the instance name.
+
*'''Name = ( string )''' - the instance name.  
* '''Status = ( integer )''' - the editor status (loading status).
+
*'''Status = ( integer )''' - the editor status (loading status).  
* '''EditorDocument = ( object )''' - the DOM Document object for the editing area.
+
*'''EditorDocument = ( object )''' - the DOM Document object for the editing area.  
* '''EditorWindow = ( object )''' - the DOM Window object for the editing area.
+
*'''EditorWindow = ( object )''' - the DOM Window object for the editing area.
  
Methods:
+
Methods:  
  
* '''AttachToOnSelectionChange( functionPointer )'''
+
*'''AttachToOnSelectionChange( functionPointer )'''  
* '''Focus()'''
+
*'''Focus()'''  
* '''GetXHTML( formatted )'''
+
*'''SetHTML( html ) - sets the contents of the editor. Note that when using this method, you will loose any listener that you may have previously registered on the editor.EditorDocument.'''
* '''InsertElement( element )'''
+
*'''GetHTML( formatted ), or GetXHTML( formatted ), or GetData( formatted ) - retrieves the edited html from the editor.'''  
* '''InsertElementAndGetIt( e )'''
+
*'''InsertElement( element )'''  
* '''InsertHtml( html )''' - inserts HTML in the current cursor position
+
*'''InsertElementAndGetIt( e )'''  
* '''IsDirty()''' - checks if the content in the editor has been changed
+
*'''InsertHtml( html )''' - inserts HTML in the current cursor position  
* '''MakeEditable()'''
+
*'''IsDirty()''' - checks if the content in the editor has been changed  
* '''ResetIsDirty()''' - resets the dirty state
+
*'''MakeEditable()'''  
* '''SetHTML( html )''' - sets the contents of the editor. Note that when using this method, you will loose any listener that you may have previously registered on the editor.EditorDocument.
+
*'''ResetIsDirty()''' - resets the dirty state  
* '''GetHTML()''' - retrieves the edited html from the editor.
+
*'''SwitchEditMode()'''  
* '''SwitchEditMode()'''
+
*'''UpdateLinkedField()'''
* '''UpdateLinkedField()'''
 
  
== Events ==
+
== Events ==
  
Once the editor loading phase is complete and it is ready to use (and interact with JavaScript), a standard function is called in the page that contains the editor, if the function is defined. This function must be named '''"FCKeditor_OnComplete"''' and receives the related editor instance as the parameter. Using it, you can execute any initial code that may interact with the editor. This is a declaration example:
+
Once the editor loading phase is complete and it is ready to use (and interact with JavaScript), a standard function is called in the page that contains the editor, if the function is defined. This function must be named '''"FCKeditor_OnComplete"''' and receives the related editor instance as the parameter. Using it, you can execute any initial code that may interact with the editor. This is a declaration example:  
 
<pre>function FCKeditor_OnComplete( editorInstance )
 
<pre>function FCKeditor_OnComplete( editorInstance )
 
{
 
{
Line 48: Line 47:
 
}
 
}
 
</pre>  
 
</pre>  
Other than the above standard function, every FCKeditor instance has an "Event" object that can be used to listen for events to be fired.
+
Other than the above standard function, every FCKeditor instance has an "Event" object that can be used to listen for events to be fired.  
  
Example: the following code listens for the "OnSelectionChange" to execute custom code:
+
Example: the following code listens for the "OnSelectionChange" to execute custom code:  
 
<pre>function FCKeditor_OnComplete( editorInstance )
 
<pre>function FCKeditor_OnComplete( editorInstance )
 
{
 
{
Line 65: Line 64:
 
}
 
}
 
</pre>  
 
</pre>  
Note that every event callback function receives the editor instance as a parameter.
+
Note that every event callback function receives the editor instance as a parameter.  
  
The following is the list of events available:
+
The following is the list of events available:  
  
* '''OnAfterLinkedFieldUpdate''' - fired right after the hidden linked field attached to the editor has its contents updated. It happens usually when the form is being posted.
+
*'''OnAfterLinkedFieldUpdate''' - fired right after the hidden linked field attached to the editor has its contents updated. It happens usually when the form is being posted.  
* '''OnAfterSetHTML''' - fired once the HTML is loaded in the editor (including when changing views).
+
*'''OnAfterSetHTML''' - fired once the HTML is loaded in the editor (including when changing views).  
* '''OnFocus''' - fired when the editor acquires the focus.
+
*'''OnFocus''' - fired when the editor acquires the focus.  
* '''OnPaste''' - fired when something is pasted in the editor. The function you specify must return true for the paste to proceed.
+
*'''OnPaste''' - fired when something is pasted in the editor. The function you specify must return true for the paste to proceed.  
* '''OnStatusChange''' - fired when the editor status changes. The following constants are also available globally in the page: FCK_STATUS_NOTLOADED, FCK_STATUS_ACTIVE and FCK_STATUS_COMPLETE.
+
*'''OnStatusChange''' - fired when the editor status changes. The following constants are also available globally in the page: FCK_STATUS_NOTLOADED, FCK_STATUS_ACTIVE and FCK_STATUS_COMPLETE.  
* '''OnSelectionChange''' - fired when the actual selection in the editor area changes (including cursor position and keystrokes). Note: In IE, this event does not fire on every keystroke, but only on some random keystrokes.
+
*'''OnSelectionChange''' - fired when the actual selection in the editor area changes (including cursor position and keystrokes). Note: In IE, this event does not fire on every keystroke, but only on some random keystrokes.
  
== Usage Samples ==
+
== Usage Samples ==
  
The following are a few samples of things that can be done with the JavaScript API. (In these samples, oEditor = FCKeditorAPI.GetInstance('InstanceName').)
+
The following are a few samples of things that can be done with the JavaScript API. (In these samples, oEditor = FCKeditorAPI.GetInstance('InstanceName').)  
  
* '''Insert HTML at cursor position''':<pre>oEditor.InsertHtml(HTML);</pre>
+
*'''Insert HTML at cursor position''':<pre>oEditor.InsertHtml(HTML);</pre>  
* '''Triggering a toolbar button / command''':<pre>oEditor.Commands.GetCommand(commandName).Execute();</pre>
+
*'''Triggering a toolbar button / command''':<pre>oEditor.Commands.GetCommand(commandName).Execute();</pre>  
* '''Disabling toolbar buttons''':<pre>oEditor.EditorWindow.parent.FCKToolbarItems.LoadedItems[commandName].Disable();</pre>
+
*'''Disabling toolbar buttons''':<pre>oEditor.EditorWindow.parent.FCKToolbarItems.LoadedItems[commandName].Disable();</pre>  
* '''Setting a config value''':<pre>oEditor.Config['&lt;configVariableName&gt;'] = 'newValue';</pre>
+
*'''Setting a config value''':<pre>oEditor.Config['&lt;configVariableName&gt;'] = 'newValue';</pre>  
* '''Change editor document style at runtime''':<pre>oEditor.EditorDocument.body.style.cssText += 'color: #322805; background-color: #F7C928;'&nbsp;;</pre>
+
*'''Change editor document style at runtime''':<pre>oEditor.EditorDocument.body.style.cssText += 'color: #322805; background-color: #F7C928;'&nbsp;;</pre>  
* '''Set or get anything of the editor''':<pre>FCKeditorAPI.GetInstance('InstanceName').EditorWindow.parent...</pre>
+
*'''Set or get anything of the editor''':<pre>FCKeditorAPI.GetInstance('InstanceName').EditorWindow.parent...</pre>  
* '''Add a dynamic Save function''':
+
*'''Add a dynamic Save function''':
<pre>
+
<pre>// called on save
// called on save
 
 
function doSave(){
 
function doSave(){
 
         alert('Saved.');
 
         alert('Saved.');
Line 98: Line 96:
 
         editorInstance.LinkedField.form.onsubmit = doSave;
 
         editorInstance.LinkedField.form.onsubmit = doSave;
 
}
 
}
</pre>
+
</pre>  
 
+
*'''Working with the selection in the editor''': Use the Selection Object
* '''Working with the selection in the editor''': Use the Selection Object
 

Revision as of 07:28, 2 July 2008

FCKeditor JavaScript API

FCKeditor offers a complete JavaScript API so you can interact with it once the editor is loaded and running.

Once loaded, the editor registers a global object called FCKeditorAPI. This object offers the entry point to interact with any editor instance placed in a page (you can have more than one).

NOTE: The FCKeditorAPI object will not be available during the page load. You need to wait for the editor to be loaded to use it. If you need to interact with the editor right after is has been loaded, use the "FCKeditor_OnComplete" function (see Events).

Retrieving an Editor Instance

  • From out of an external script
    When placing the editor in the page, you give it an "instance name". To retrieve it, you must simply call the FCKeditorAPI.GetInstance method.
    Example:
    var oEditor = FCKeditorAPI.GetInstance('InstanceName') ;
  • From out of a dialog of the editor
    Call the InnerDialogLoaded to get the FCKObject.
    Example:
    var oEditor = window.parent.InnerDialogLoaded().FCK ;

Both methods return the main FCKeditor object that gives the necessary bridge to interact with it. These are the most useful properties and methods of this object:

Properties:

  • Name = ( string ) - the instance name.
  • Status = ( integer ) - the editor status (loading status).
  • EditorDocument = ( object ) - the DOM Document object for the editing area.
  • EditorWindow = ( object ) - the DOM Window object for the editing area.

Methods:

  • AttachToOnSelectionChange( functionPointer )
  • Focus()
  • SetHTML( html ) - sets the contents of the editor. Note that when using this method, you will loose any listener that you may have previously registered on the editor.EditorDocument.
  • GetHTML( formatted ), or GetXHTML( formatted ), or GetData( formatted ) - retrieves the edited html from the editor.
  • InsertElement( element )
  • InsertElementAndGetIt( e )
  • InsertHtml( html ) - inserts HTML in the current cursor position
  • IsDirty() - checks if the content in the editor has been changed
  • MakeEditable()
  • ResetIsDirty() - resets the dirty state
  • SwitchEditMode()
  • UpdateLinkedField()

Events

Once the editor loading phase is complete and it is ready to use (and interact with JavaScript), a standard function is called in the page that contains the editor, if the function is defined. This function must be named "FCKeditor_OnComplete" and receives the related editor instance as the parameter. Using it, you can execute any initial code that may interact with the editor. This is a declaration example:

function FCKeditor_OnComplete( editorInstance )
{
    alert( editorInstance.Name ) ;
}

Other than the above standard function, every FCKeditor instance has an "Event" object that can be used to listen for events to be fired.

Example: the following code listens for the "OnSelectionChange" to execute custom code:

function FCKeditor_OnComplete( editorInstance )
{
    editorInstance.Events.AttachEvent( 'OnSelectionChange', DoSomething ) ;
}

var counter = 0 ;

function DoSomething( editorInstance )
{
    // This is a sample function that shows in the title bar the number of times
    // the "OnSelectionChange" event is called.
    window.document.title = editorInstance.Name + ' : ' + ( ++counter ) ;
}

Note that every event callback function receives the editor instance as a parameter.

The following is the list of events available:

  • OnAfterLinkedFieldUpdate - fired right after the hidden linked field attached to the editor has its contents updated. It happens usually when the form is being posted.
  • OnAfterSetHTML - fired once the HTML is loaded in the editor (including when changing views).
  • OnFocus - fired when the editor acquires the focus.
  • OnPaste - fired when something is pasted in the editor. The function you specify must return true for the paste to proceed.
  • OnStatusChange - fired when the editor status changes. The following constants are also available globally in the page: FCK_STATUS_NOTLOADED, FCK_STATUS_ACTIVE and FCK_STATUS_COMPLETE.
  • OnSelectionChange - fired when the actual selection in the editor area changes (including cursor position and keystrokes). Note: In IE, this event does not fire on every keystroke, but only on some random keystrokes.

Usage Samples

The following are a few samples of things that can be done with the JavaScript API. (In these samples, oEditor = FCKeditorAPI.GetInstance('InstanceName').)

  • Insert HTML at cursor position:
    oEditor.InsertHtml(HTML);
  • Triggering a toolbar button / command:
    oEditor.Commands.GetCommand(commandName).Execute();
  • Disabling toolbar buttons:
    oEditor.EditorWindow.parent.FCKToolbarItems.LoadedItems[commandName].Disable();
  • Setting a config value:
    oEditor.Config['<configVariableName>'] = 'newValue';
  • Change editor document style at runtime:
    oEditor.EditorDocument.body.style.cssText += 'color: #322805; background-color: #F7C928;' ;
  • Set or get anything of the editor:
    FCKeditorAPI.GetInstance('InstanceName').EditorWindow.parent...
  • Add a dynamic Save function:
// called on save
function doSave(){
        alert('Saved.');
        document.getElementById('someElement').innerHTML = 'Saved!';
        return false; //this disables default action (submitting the form)
}
// called when FCKeditor is done starting..
function FCKeditor_OnComplete( editorInstance ){
        editorInstance.LinkedField.form.onsubmit = doSave;
}
  • Working with the selection in the editor: Use the Selection Object