Friday, May 12, 2006

Using FCKEditor in AJAX


The FCKEditor is one of the best opensource compenents to reach the web.  Written purely in Javascript/DHTML, this rich text editor brings forth a whole new level of website management, allowing you to use a Word-like interface to manage web content.  We've talked about the benefits of the FCKEditor before (checkout WYSIWYG Of The Stars). 

Now, we've been working on taking the next step though.  What if you want to open the editor using AJAX without doing a page refresh?  And what if you also wanted to submit the revised content using an AJAX call as well?  Dig no further, the Zunch Geek Squad has found a workable answer :-)  First, lets review the problems...

Typically, you'd create the instance of the editor by creating the proper iframe element and a hidden input element.  When you first create the iframe dynamically using AJAX, it's not really that big of a deal, everything loads just as you'd expect.  The first problem is that you can't just query the hidden input element for the current value of the editor - that was set when the page loaded, and the hidden input field hasn't been updated since then.  To pull the content out of the editor to send in through an AJAX call, use this Javascript code:

var content = FCKeditorAPI.GetInstance('HiddenInputName'').GetXHTML();

The next problem you'll encouter is that Firefox doesn't seem to like to close and then reopen the editor without refreshing the page first.  This is a simple issue of certain variables already being intialized, so all ya have to do to work around it is destroy the interferring variables.  Turns out the two offending variables can be simply set to Null before ya load the iframe!

FCKeditorAPI = null;
__FCKeditorNS = null;


Now that we've figured out that little nuscance, be expecting to see alot more interactivity in our applications.  We're working hard to build the next generation of applications that will keep your website easily updated and flooded with potential customers. 

PermaLink


Superb touch

07/18/06 @ 08:45 AM | Posted By bandeng

This article is great, thanks. I solve my problem with my experiment php ajax.

I load the whole page into innerHTML and I created iframe and hidden input. unfortunately no javascript is not allowed to run when you load into innerHTML.

With help of another IFRAME I put on top body, all script I execute from there and need a bit modified to remove the instance on src of IFRAME:

parent.FCKeditorAPI = null;
parent.FCKeditorNS = null;

Awesome!

06/11/06 @ 06:53 PM | Posted By Erik

This worked out beautifully!

Thanks!

Glad we could help!

06/06/06 @ 06:23 PM | Posted By Scott Wilson

Glad you found this tip useful - thanks for reading!!!

.

06/02/06 @ 05:35 AM | Posted By iceberg

*Thanks ;-)

Guys, you're great

06/02/06 @ 05:35 AM | Posted By iceberg

I've been searching for solution of this problem for few days.
Your solution is just perfect!

Thenks!



Title: