Jump to content

Selecting text in an iframe?


jmace

Recommended Posts

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. :-)

Link to comment
https://forums.phpfreaks.com/topic/226208-selecting-text-in-an-iframe/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.