Getting IFRAME text

A

Aaron Gray

<iframe name="iframe" width="100%" height="25%" src="test1.txt">
</iframe>

<a href="test1.txt" target="input">one</a>
<a href="test2.txt" target="input">two</a>

<form name="form1">
<textarea name="textarea" cols=80 rows=18>
This is a test
</textarea><br>
<button name="Copy" value="Copy" OnClick="DoCopy()">Copy</button>
</form>

<script>

function DoCopy()
{
form1.textarea.value=iframe.innerText;
}
</script>
 
A

Aaron Gray

Whoops, forgot the question !

I want to copy the text from a text document opened in an iframe into a
textarea at th press of a button.
<iframe name="iframe" width="100%" height="25%" src="test1.txt">
</iframe>

<a href="test1.txt" target="input">one</a>
<a href="test2.txt" target="input">two</a>

<form name="form1">
<textarea name="textarea" cols=80 rows=18>
This is a test
</textarea><br>
<button name="Copy" value="Copy" OnClick="DoCopy()">Copy</button>
</form>

<script>

function DoCopy()
{
form1.textarea.value=iframe.innerText;
}
</script>

I tried 'iframe.body.innerText' but that did not work.

Aaron
 
V

VK

Aaron said:
Whoops, forgot the question !

I want to copy the text from a text document opened in an iframe into a
textarea at th press of a button.


I tried 'iframe.body.innerText' but that did not work.

Aaron


<iframe name="iframe01" width="100%" height="25%" src="test1.txt">
</iframe>

<form name="form1">
<textarea name="txt01" cols=80 rows=18>
This is a test
</textarea><br>
<button name="Copy" value="Copy" OnClick="DoCopy()">Copy</button>
</form>


<script>


function DoCopy()
{
document.forms['form1'].elements['txt01'].value =
document.frames['iframe01'].document.body.innerHTML;
}
</script>



1) Never ever give the names/id using generic html element names
("iframe", "textarea" etc.)

2) <http://www.javascripttoolbox.com/bestpractices/> see about
addressing frames/form elements - helps a lot.

;-)
 
M

Martin Honnen

Aaron said:
<iframe name="iframe" width="100%" height="25%" src="test1.txt">
</iframe>

Is that really a text/plain document in the iframe? Or a text/html
document? Obviously the DOM is defined for (structured) HTML and XML
documents but not for (unstructured) text documents.
Though some browser (IE, Mozilla too perhaps) might expose a document
with a body for text, see below.
<form name="form1">
<textarea name="textarea" cols=80 rows=18>
This is a test
</textarea><br>
<button name="Copy" value="Copy" OnClick="DoCopy()">Copy</button>

<input type="button" value="Copy"
onclick="DoCopy(this.form.elements.textarea, 'iframe')">

function DoCopy()

function DoCopy (textControl, iframeName) {
if (window.frames && window.frames[iframeName] &&
window.frames[iframeName].document &&
window.frames[iframeName].document.body &&
window.frames[iframeName].document.body.innerHTML)
{
textControl.value =
window.frames[iframeName].document.body.innerHTML;
}
}
 
A

Aaron Gray

<input type="button" value="Copy"
onclick="DoCopy(this.form.elements.textarea, 'iframe')">

function DoCopy (textControl, iframeName) {
if (window.frames && window.frames[iframeName] &&
window.frames[iframeName].document &&
window.frames[iframeName].document.body &&
window.frames[iframeName].document.body.innerHTML)
{
textControl.value =
window.frames[iframeName].document.body.innerHTML;
}
}

Great, the text appears as a <pre> block, but using InnerText rather than
innerHTML sorts that out and just copies the raw text :)

Thanks alot,

Aaron
 
R

Randy Webb

Aaron Gray said the following on 11/10/2005 3:57 PM:
<input type="button" value="Copy"
onclick="DoCopy(this.form.elements.textarea, 'iframe')">

function DoCopy (textControl, iframeName) {
if (window.frames && window.frames[iframeName] &&
window.frames[iframeName].document &&
window.frames[iframeName].document.body &&
window.frames[iframeName].document.body.innerHTML)
{
textControl.value =
window.frames[iframeName].document.body.innerHTML;
}
}


Great, the text appears as a <pre> block, but using InnerText rather than
innerHTML sorts that out and just copies the raw text :)

And only works in IE since innerText is IE proprietary code.
 
T

Thomas 'PointedEars' Lahn

Aaron said:

Don't listen to what V'often wrong'K is telling you. There is nothing
wrong in using such names, there is no known side effect in any language
or UA. What is true is that it is a Good Thing to choose names with
meaning; for example, the `textarea' element could be named according
to its expected content.
This does not deal with getting the text from a text document
in an iframe which is what I was after.

Try referenceToIframe.document.body.firstChild.firstChild.nodeValue in
Mozilla/5.0. WFM in Firefox 1.0.7/Linux as an iframe with a plaintext
resource is made available through an embedded HTML document with a
`pre' element containing the resource's content.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Aaron said:
Great, the text appears as a <pre> block, but using InnerText rather than
innerHTML sorts that out and just copies the raw text :)

Note that innerText (JS is case-sensitive) is an IE-proprietary property.


PointedEars
 
A

Aaron Gray

Don't listen to what V'often wrong'K is telling you. There is nothing
wrong in using such names, there is no known side effect in any language
or UA. What is true is that it is a Good Thing to choose names with
meaning; for example, the `textarea' element could be named according
to its expected content.

Yes they are different namespaces.
Try referenceToIframe.document.body.firstChild.firstChild.nodeValue in
Mozilla/5.0.
WFM in Firefox 1.0.7/Linux as an iframe with a plaintext
resource is made available through an embedded HTML document with a
`pre' element containing the resource's content.

referenceToIframe.document.body.firstChild.firstChild.nodeValue

Works in IE too :)

Thanks.

Aaron
 
T

Thomas 'PointedEars' Lahn

Aaron Gray wrote:

Please provide proper attribution, see
referenceToIframe.document.body.firstChild.firstChild.nodeValue

Works in IE too :)

Nice :) However it is not supposed to work in IE/Windows < 5.0 and I'm
not sure about IE/Mac either. W3C DOM support was introduced in IE pretty
late, so you should definitely feature-test it on run time.

You're welcome.


PointedEars
 
A

Aaron Gray

Why, I did not top-post :)

Just joking :)

I did think when I wrote that post that the context was odd.

Aaron
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top