PDA

View Full Version : How to select iFrame contents?



Tali
03-08-2005, 08:48 AM
I am trying to make a JavaScript highlight the contents of an iFrame. It is easy enough when I use a textarea, but I can't seem to get it to work in an iFrame.


My frame id is "external". The following seems to reference my iFrame, but it doesn't select the contents, it just moves the page so the iFrame is all in the page (focused?).

javascript:"document.all.external.focus();document.all.external.select()"

jpcarusi
03-26-2005, 03:26 PM
Hi Tali,

I have exactly the same problem...

but i think im near to find the solution, because the "focus" is already working... now we just need to find the correct command to select all and copy...

<html><head><title>JavaScript code/Clipboard</title>
<script language="JavaScript"><!--


function SelecionarTexto(){
document.form1.textarea1.focus();
document.form1.textarea1.select();
}

--></script></head>
<body>




<IFRAME SRC="http://www.google.com.br" WIDTH="100%" HEIGHT="60" onload="focus();">
<!-- Alternate content for non-supporting browsers -->
</IFRAME>


<input type='button'onClick="SelecionarTexto()">

<form name="form1">
<textarea cols="60" rows="8" name="textarea1">
This is the textarea content. Change it to suit...
</textarea>
</form>
</body></html>

ddadmin
03-26-2005, 05:44 PM
Are you trying to select a textarea within the iframe, or the entire page? The later would be more difficult. If the former though, something like the below should work:


var myiframe=document.getElementById("aiframe")
if (myiframe.contentDocument) //ns6 syntax
myiframe.contentDocument.myform.mytextarea.select()
else if (myiframe.Document) //ie5+ syntax
myiframe.Document.myform.mytextarea.select()
where "aiframe" is the ID of your iframe, and "myform" and "mytextarea", the name of your form and textarea.