PDA

View Full Version : text selection in iframe?



jmace
01-31-2011, 07:39 AM
Does anyone know anything about selecting text in an editable iframe? The following code kind of works in FireFox (it's a little buggy), but doesn't work at all in anything else.



<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>ADVANCED EDITOR TEST PAGE</title>
<script type="text/javascript">
function framego(){
document.getElementById("frame").contentDocument.designMode = "On";
document.getElementById("frame").contentWindow.document.body.innerHTML = "TEST [here] ING";
}
function selectArea(){
o=document.getElementById('frame');

if(o.contentWindow.getSelection) sel = o.contentWindow.getSelection(); //FF
else sel = o.contentDocument.selection.createRange(); //IE

range2=sel.getRangeAt(0);

var range = document.createRange();
range.setStart(sel.anchorNode,5);
range.setEnd(sel.anchorNode,11);

//remove the old range and add the newly created range
sel.removeRange(range2);
sel.addRange(range);

o.focus();
}
</script>
</head>
<body onload="framego()">
<iframe id="frame"></iframe>
<button onclick="selectArea()">TEST</button>
</body>
</html>


It's supposed to select the word "Here" in the iframe.

A million thanks to anyone who can point me in the right direction on this one. :-)