views:

589

answers:

3

FCKeditor has InsertHtml API (JavaScript API document) that inserts HTML in the current cursor position. How do I insert at the very end of the document?

Do I need to start browser sniffing with something like this

if ( element.insertAdjacentHTML )    // IE 
    element.insertAdjacentHTML( 'beforeBegin',  html ) ; 
else                                // Gecko 
{ 
    var oRange = document.createRange() ; 
    oRange.setStartBefore( element ) ; 
    var oFragment = oRange.createContextualFragment( html ); 
    element.parentNode.insertBefore( oFragment,  element ) ; 
}

or there is a blessed way that I miss?

Edit: Of course, I can rewrite the whole HTML, as answers suggest, but I cannot believe that is the "blessed" way. That means that browser should destroy whatever it has and re-parse the document from scratch. That cannot be good. For example, I expect that to break the undo stack.

A: 

document.body.appendChild?

Andy
+2  A: 

It looks like you could use a combination of GetHTML and SetHTML to get the current contents, append your html and reinsert everything into the editor. Although it does say

Note that when using this method, you will loose any listener that you may have previously registered on the editor.EditorDocument.

Hope that helps!

Ryan Doherty
+1  A: 

var oEditor = FCKeditorAPI.GetInstance('Editor_instance') ; OldText=oEditor.GetXHTML( true );

oEditor.SetData( OldText+"Your text");