I'm really struggling with a Strofvver problem: In Internet Explorer, I want to insert plain text to the current caret position. This simple reader works really well for elements but it does not work for completely editable IFRAMEs, which is what I have
I use the script IFRAME I am creating a TextRange object from the document that I use to paste the text as HTML in cursor position.
& lt; Iframe id = "editable" & gt; & Lt; Html & gt; & Lt; Body & gt; Some really boring text & lt; / Body & gt; & Lt; / Html & gt; & Lt; / Iframe & gt; & Lt; Script type = "text / javascript" & gt; Window.onload = function () {var iframe = document.getElementById ('editable'); Var Doctor = iframe.contentDocument || Iframe.contentWindow.document; Doc.body.innerHTML = iframe.textContent || Iframe.innerHTML; // Make IFRAME editable if (doc.body.contentEditable) {doc.body.contentEditable = true; }} Enter function (text) {var iframe = document.getElementById ('editable'); Var Doctor = iframe.contentDocument || Iframe.contentWindow.document; Iframe.focus (); If (type doc.selection! = 'Undefined') {var range = doc.selection.createRange (); Range.pasteHTML (text); }} & Lt; / Script & gt; & Lt; Input type = "button" value = "insert" onClick = "insert ('foo');" /> When I select some text in IFRAME, the selection will be replaced with "FU" - this is expected behavior. But when I keep a carpet somewhere in the text, the inclusion will not work.
Is this a normal practice, because there is no "no real selection" for this matter that I have somewhere a cursor somewhere or is it a bug with editable iFRAMEs in IE because this simple text Works very well with the elements?
Is there any remedy?
If you press onclick instead of onclousedown Code>, you can do this work. Update The reason for this is that iframe has lost focus Click the click event, while there is a fire before MoSDown . More updates You can try to fix it in IE by saving / reinstalling the selected text range / loses the iframe / Focus It does something like this:
function fixIframeCaret (iframe) {if (iframe.attachEvent) {var selectedRange = null; Iframe.attachEvent ("onbeforedeactivate", function () {var sel = iframe.contentWindow.document.selection; if (sel.type! = "None") {selectedRange = sel.createRange ();}}); Iframe.contentWindow.attachEvent ("onfocus", function () {if (selectedRange) {selectedRange.select ();}}); }} Window.onload = function () {var iframe = document.getElementById ('editable'); FixIframeCaret (iframe); };
Comments
Post a Comment