When I bring up jQuery dialog prompt for inserting a picture, I lose the caret position in my designmode on iFrame. How would I go about inserting the picture at the previous caret position without having to re-place the caret by hand before submitting? I am pretty sure it has to do with getting the range, because on default it selects the start of the frame.
Here is my code- It works fine only you have to select the position to place the image once again after pasting the URL in the prompt.
var iframe = document.getElementById('editor');
var iframeDocument = document.getElementById('editor').contentDocument || document.getElementById('editor').contentWindow.document;
iframe.contentWindow.focus()
var sel, range;
if (iframe.contentWindow.getSelection) {
sel = iframe.contentWindow.getSelection();
if (sel.getRangeAt && sel.rangeCount) {
range = sel.getRangeAt(0);
range.deleteContents();
var el = document.createElement("div");
var el = iframe.contentDocument.createElement("div");
el.innerHTML = html;
var frag = iframe.contentDocument.createDocumentFragment(), node, lastNode;
while (node = el.firstChild) {
lastNode = frag.appendChild(node);
}
range.insertNode(frag);