Ie8 Iframe Designmode Loses Selection
Solution 1:
On any element in the main document you want not to break the iframe selection, add unselectable="on"
.
For example:
<divonclick="makeBold()"unselectable="on">Bold</div>
Solution 2:
You could try saving the selection when the iframe loses focus. If you're sure the content of the iframe will not change before the user focuses on it again, you can store the currently selected Range
or TextRange
. The following script (for the main page) includes your original script, is not extensively tested and would be improved with better feature detection but is something to work with:
h_FF=!document.all
h_rt_F=0functionHLC_DM()
{
h_rt_F=document.getElementById("moo").contentWindowif(h_FF)
{
if(h_rt_F.document.designMode!="on")
{
try
{
h_rt_F.document.designMode="on"
h_rt_F.document.execCommand("redo",false,null)
createEventHandlers();
}
catch(e)
{
setTimeout("HLC_DM",200)
returnfalse
}
}
}
else
h_rt_F.document.body.contentEditable=truecreateEventHandlers();
}
functiongetContentWindow() {
returndocument.getElementById("moo").contentWindow;
}
functionsaveSelection() {
var win = getContentWindow();
var doc = win.document;
var sel = win.getSelection ? win.getSelection() : doc.selection;
var range;
if (sel) {
if (sel.createRange) {
range = sel.createRange();
} elseif (sel.getRangeAt) {
range = sel.getRangeAt(0);
} elseif (sel.anchorNode && sel.focusNode && doc.createRange) {
// Older WebKit browsers
range = doc.createRange();
range.setStart(sel.anchorNode, sel.anchorOffset);
range.setEnd(sel.focusNode, sel.focusOffset);
// Handle the case when the selection was selected backwards (from the end to the start in the// document)if (range.collapsed !== sel.isCollapsed) {
range.setStart(sel.focusNode, sel.focusOffset);
range.setEnd(sel.anchorNode, sel.anchorOffset);
}
}
}
return range;
}
functionrestoreSelection(range) {
var win = getContentWindow();
var doc = win.document;
var sel = win.getSelection ? win.getSelection() : doc.selection;
if (sel && range) {
if (range.select) {
range.select();
} elseif (sel.removeAllRanges && sel.addRange) {
sel.removeAllRanges();
sel.addRange(range);
}
}
}
var selectedRange;
functionblurHandler() {
selectedRange = saveSelection();
}
functionfocusHandler() {
if (selectedRange) {
restoreSelection(selectedRange);
}
}
var iframeHandlersCreated = false;
functioncreateEventHandlers() {
// Prevent setting up twiceif (!iframeHandlersCreated) {
var iframe = document.getElementById("moo");
var doc;
if (iframe.contentDocument && iframe.contentDocument.addEventListener) {
doc = iframe.contentDocument;
doc.addEventListener("blur", blurHandler, false);
doc.addEventListener("focus", focusHandler, false);
} elseif (iframe.attachEvent) {
iframe.attachEvent("onbeforedeactivate", blurHandler);
iframe.attachEvent("onfocus", focusHandler);
}
iframeHandlersCreated = true;
}
}
Solution 3:
My Editbox can add images, tables etc where you last clicked in the iframe and works for ie6, ie7 and FF but for ie8 it adds then at the start. They can then be cut and pasted to where you want them but that is a nuisance. MORE SERIOUS is that when I want to change the attributes of a table cell, for example, I have to have some text now in the cell which I must highlight or I can't determine what element I'm in!
Have Microsoft any bug fixes for the selection method or is Firefox or old versions of ie the only course?
regards Mike W
Post a Comment for "Ie8 Iframe Designmode Loses Selection"