Inserting HTML at the current cursor position in an editable iframe (designMode =”on”)

9 01 2008

We have a requirement, where we need to insert some HTML into a telerik RAD editor control, at the current cursor position.

I thought this was going to be a peice of cake.  Firstly, I didn’t realise that the telerik RAD editor control actually used an iframe, with an internal DOM set to designMode = “on”.  This basically instantiates a document in “edit” mode, and within internet explorer (and I believe this works for most other browsers) will let you edit and paste HTML content – a likely choice for most hardcore text-editors for the web.

After finding this information out – I stumbled upon an awesome read, how this guy had created a javascript syntax highlighter, through the means of a DOM in designmode, and javascript itself.  He ended up writing an entire parser, lexer, and tokenizer within javascript to interpret the entire script to make the highlighter work nicer, and he also managed to make it aware of scoped variables…cool.
http://marijn.haverbeke.nl/codemirror/story.html

Anyhow, I started studying the various ways he went about implementing the selection system.
I ended up, after all of my study, working out how it can be done – and yes, as usual, it’s one line of code, and a very simple operation.  I have only tested this in IE so far:
document.getElementById(‘myEditorIframe’).document.selection.createRange().pasteHTML(“<a href=’http://www.google.com.au’>My pdf name</a>”);

Brilliant!  Now all I need to do is create a tiny bit of client side script to do the above, and I’ll be laughing!
Hope this helps someone else out!

M

EDIT:

Alright, so this doesn’t work for firefox, I get it :)   It also doesn’t work for IE7, by the looks of it.  In any case, I scoured the net and found this function which seems to work in firefox + IE6, haven’t tested it in IE7 (but I doubt it will work).

rteName = “Name of your editor IFRAME”;
function rteInsertHTML(html) {
if (document.all) {
var oRng = document.getElementById(rteName).contentWindow.document.selection.createRange( );
oRng.pasteHTML(html);
oRng.collapse(false);
oRng.select();
} else {
document.getElementById(rteName).contentWindow.document.execCommand(‘insertHTM L’, false, html);
}
};


Actions

Information

9 responses

20 02 2008
yc

Hi –

Did you figure out the firefox version? I had trouble googling even a reference – here are the issues:

1) selectionStart & selectionEnd disappeared in iframe
2) empty selection cannot be used to create range
3) empty selection also cannot add range

Thanks,

7 05 2008
Jen

It’s really really help me a lot!!!
Thank you very much!

4 02 2009
x

xxxxxx

5 04 2009
juancho

Gracias por el codigo!!! justo lo que necesitaba!!

28 05 2009
Howard

Great work!

In a , you can’t use “selection” so that the same method doesn’t work. Do you know a way to do the same thing in editable ?

28 05 2009
Howard

Great work!

In <div>, you can’t use “selection” so that the same method doesn’t work. Do you know a way to do the same thing in editable <div>? Thanks.

23 06 2009
Fahad Umar

It really helped me a lot.

thanks

3 07 2009
vivek

this is not working in IE8. :(

23 08 2009
Fred

Thank you, it helped me too

but doesn’t work on firefox, don’t you know how to do it?

Leave a comment