FAQ Topic - How do I access a frame's content? (2009-10-25)

T

Thomas 'PointedEars' Lahn

Garrett said:
Dr said:
In comp.lang.javascript message <[email protected]
september.org>, Wed, 28 Oct 2009 12:16:06, Garrett Smith


In a FAQ, answers should be classified according to the nature of the
question, not according to the nature of the answer.

A common reasoning for questioning is that the answer is not where the
questioner thought to look.

In ordinary English, a frame, or at least an iframe, does commonly have
an innerText or textProperty property; but it is kept by one of its
descendants.

The IFRAME element and the frame (as in frames[0] or
anIframe.contentWindow), are different.

It is important to be clear on the distinction. These are technical
terms, not ordinary English terms.

Don't be ridiculous. The standards-compliant `textContent' property of the
object referred to by the standards-compliant `contentDocument' property of
an `iframe' element object (which is the same as the object referred to by
the `document' property of the object referred to by the proprietary
`contentWindow' property of an `iframe' element object) undoubtedly provides
access to the `iframe''s content.

So you are being deliberately obtuse.
The "content" would be in the document.

And if the document would be a plain text document, the `textContent'
property of wrapping document element object would yield exactly its
content.


PointedEars
 
G

Garrett Smith

Thomas said:
Garrett said:
Dr said:
In comp.lang.javascript message <[email protected]
september.org>, Wed, 28 Oct 2009 12:16:06, Garrett Smith
<[email protected]> posted:
As I explained, the innerText/textContent is not related to the frame;
it is related to elements. An entry on getting frame's
innerText/textContent implies that a frame has a property
innerText/textContent. That would be false.
In a FAQ, answers should be classified according to the nature of the
question, not according to the nature of the answer.

A common reasoning for questioning is that the answer is not where the
questioner thought to look.

In ordinary English, a frame, or at least an iframe, does commonly have
an innerText or textProperty property; but it is kept by one of its
descendants.
The IFRAME element and the frame (as in frames[0] or
anIframe.contentWindow), are different.

It is important to be clear on the distinction. These are technical
terms, not ordinary English terms.

Don't be ridiculous. The standards-compliant `textContent' property of the
object referred to by the standards-compliant `contentDocument' property of
an `iframe' element object (which is the same as the object referred to by
the `document' property of the object referred to by the proprietary
`contentWindow' property of an `iframe' element object) undoubtedly provides
access to the `iframe''s content.

The iframe contents could be an image, an HTML document with a
Flash movie, javascript, a canvas.

The IFRAME content, in those cases, is what?
frame.contentWindow.document.textContent? Wow. You're so smart.

In contrast, there is a node that has a textContent or innerText
property. That node may have a document, which may have a parentWindow,
and the |parent| of that parentWindow may be including that resource via
an IFRAME or FRAME element.

So you are being deliberately obtuse.
No, I am correct.

As long as I have something to say about it, the entry will correctly
explain how to access the window object of the IFRAME.

| A frame's content window can be found from the frames collection.

And from there, the window has a document, which has a |getElementById|,
a |forms| property, etc. All standard fare, basic stuff.
And if the document would be a plain text document, the `textContent'
property of wrapping document element object would yield exactly its
content.
DOM properties on elements in a plain text document?

Browsers do create HTML documents for text/plain documents and give a
quirks mode dom.

It isn't standard, though, is it?
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>,
Thu, 29 Oct 2009 19:50:30, Thomas 'PointedEars' Lahn
Because I was not aware of its existence to date (obviously I never had a
problem that required searching the comp.lang.javascript FAQ for it),

There have been sections in the FAQ on frames for *at least* four years
(w.e.f. Thursday next); Google and Wayback will show many instances.

You very frequently advise people, in a peremptory manner, to search the
archives before posting.

You will have seen posted here, in FAQ sections and elsewhere,
injunctions to read the FAQ.

The FAQ begins with an index to its sections and subsections, naming
their Subjects.

Those subsections will have been posted to the newsgroup on a regular
basis by Bart.

Therefore, you should have KNOWN that the FAQ contained material on
frames; and, in any case, you should have suspected it.

Hypocrite.

I have asked before because that implication of yours would be incorrect.
Firefox/Gecko does not lack what others have; IE/MSHTML does lack it. While
probably not in number of installations, `textContent' is the standards-
compliant approach which is supported by more current layout engines than
`innerText' is or is going to be.

Firefox apparently lacks what the most-used browser (MSIE), and three
other major PC browsers, have - innerText of iframe content.

It is of course also a pity that IE lacks textContent - however, it is
unreasonable to expect (except in Nelson's context) Microsoft to heed
international standards.

Sorry, I am not familiar with that acronym, and it is not listed on
<http://www.merlyn.demon.co.uk/acronyms.htm#Acro> or in the usual places.

Indeed not. You are incorrect in assuming it there to be an acronym.
It is a well-known long-established English word, derived from the Norse
: according to Google, the Norwegians and Icelanders still use it. You
have just proved it apt. (As an acronym, it can be found at
<http://www.acronymfinder.com/OAF.html>, with 14 meanings.)

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
september.org>, Thu, 29 Oct 2009 22:11:19, Garrett Smith
As long as I have something to say about it, the entry will correctly
explain how to access the window object of the IFRAME.

You are supposed, as FAQ maintainer, to be sustaining something useful
to the ordinary questioners, especially those who are not full-time
professional JavaScript programmers.

However, you appear entirely unable to understand their positions and
points of view. FAQ maintaining is a task for the sympathetic
communicator; not for the nerd.
 
G

Garrett Smith

Dr said:
In comp.lang.javascript message <[email protected]>,
Thu, 29 Oct 2009 19:50:30, Thomas 'PointedEars' Lahn
[snip]
I have asked before because that implication of yours would be incorrect.
Firefox/Gecko does not lack what others have; IE/MSHTML does lack it. While
probably not in number of installations, `textContent' is the standards-
compliant approach which is supported by more current layout engines than
`innerText' is or is going to be.

Firefox apparently lacks what the most-used browser (MSIE), and three
other major PC browsers, have - innerText of iframe content.

The innerText property is a property of an Element. It is read/write
on most, but read only on a few elements.

<URL: http://msdn.microsoft.com/en-us/library/ms533899(VS.85).aspx >

Explains. You can disregard the garbage code by MSFT, which relies on
global scope pollution (element ID becomes globally accessible
property).

The DOM 3 Node textContent property:
http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-textContent

Includes nodeValue when accessed from a CDATASection Node or Comment
Node (I haven't found need for that yet).
It is of course also a pity that IE lacks textContent - however, it is
unreasonable to expect (except in Nelson's context) Microsoft to heed
international standards.
MSFT decided not to implement textContent in IE8, for undisclosed
reasons.

https://connect.microsoft.com/IE/feedback/ViewFeedback.aspx?FeedbackID=334438
 
V

VK

Give or take irrelevant questions of character coding and newline
representation, I have been getting, by using innerHTML and by using
innerText, a string which agrees visually with the content of a TXT file
on disc, as would be shown by Notepad.

It should be expected in many (but not all) situations.
Contrary to the popular believe, browsers are *not* able to open text
or graphics files. What they are able to - as part of their extended
functionality - is to recognize some file types other than HTML and to
wrap them on the fly into predefined HTML templates so to display them
in the browser window. In the particular for text/plain files they are
using template
<HTML>
<HEAD></HEAD>
<BODY>
<PRE> text file content goes here </PRE>
</BODY>
</HTML>
with the exact tags' case (upper or lower) being browser dependent.
This way the text you "see" is in effect the content of a single <pre>
element necessarily altered from the "as it is on disc" to be placed
into this tag. For instance all less-than and greater-than signs will
be converted to the corresponding named HTML entities. The fact that
you were getting so far "by using innerHTML ..., a string which agrees
visually with the content of a TXT file" suggests that so far you were
lucky but not having any problematic characters in your .txt files,
otherwise see my post about NE (named entities) and NCR (numeric
character references) in HTML:
http://groups.google.com/group/comp.lang.javascript/msg/7a7cb7a5ca4ad387
I still WANT to read a HTML file correspondingly, seeing the source
lines as per Notepad; but I don't really NEED to, since what I can get
suffices for that part of the desired work that is NECESSARY.

The answer remains the same: in order to do so reliably and "as it is
on disk" and not as preprogrammed by this or that browser team, you
have to use XHR (aka AJAX, aka Ajax) and its responseText property
content. Any (i)frames are useless for the task and I hope I made it
clear why. It also solves your reading access synchronization problem,
because with XHR you will be prompted when the content is available or
prompted for the file access error.
Yes, that is what I still WANT.  But it is more than, in the case of an
HTML page, I really NEED.  Therefore progress is being made.

The realization of the actual need and a proper description of this
need is a prerequisite step toward the satisfaction of the need, so
yes, an important progress is being made :)
The satisfaction way is already given a bit above.
Associated query : I have read a TXT file from disc, getting a matching
string.  It consists of many lines containing words separated by
punctuation.  They all start with the same sequence of words and
punctuation (improbably, zero length), but after that there is always
non-zero length.  No two lines completely agree.  What is the nicest way
of determining the common part AND obtaining in sequence strings for the
varying parts?  Think of it as like a representation of a directory
tree.

This is OT to the discussed FAQ topic but an interesting problem per
se. I am thinking to move it into separate thread or you may do it
yourself. I have a rather close request for ggNoSpam, in order to give
users an ability to adjust the regexp spam filter even with zero
knowledge of regular expressions. The abstract task description would
be:
"Given an array of strings with the minimum 2 and the maximum 1o
elements, find the shortest common word in these strings. If no such
common character sequence found, then try to find the biggest subset
of strings having a common word".

"word" is understood in regexp terms. To avoid "rush answers" with
common words like "a" or "the" articles let's define that the shortest
common word must be no shorter than 4 characters.


P.S. booh!
:)
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
september.org>, Fri, 30 Oct 2009 16:34:17, Garrett Smith
Dr J R Stockton wrote:

The innerText property is a property of an Element. It is read/write
on most, but read only on a few elements.
...

You miss the point, and write irrelevancies. In the context of my
linxchek.htm function ReadDirectoryFile :-

IE works with innerText, but not with textContent.
FF works with textContent, but not with innerText.
Opera, Safari, and Chrome work with either.

It is a pity, and unexpected, that Firefox differs from IE and three
other well-known browsers.

It is a pity, but not unexpected, that IE differs from four other well-
known browsers.

And that is entirely independent of the standards, the mechanism, and
the reasons if any.
 
D

Dr J R Stockton

In comp.lang.javascript message <53e60b5e-ddec-4b97-aa14-64c31f883159@j1
9g2000yqk.googlegroups.com>, Sat, 31 Oct 2009 10:08:13, VK
It should be expected in many (but not all) situations.
Contrary to the popular believe, browsers are *not* able to open text
or graphics files. What they are able to - as part of their extended
functionality - is to recognize some file types other than HTML and to
wrap them on the fly into predefined HTML templates so to display them
in the browser window. In the particular for text/plain files they are
using template
<HTML>
<HEAD></HEAD>
<BODY>
<PRE> text file content goes here </PRE>
</BODY>
</HTML>
with the exact tags' case (upper or lower) being browser dependent.

I wrote "getting, ..., a string", not "saw in a window".

Fram being a reference to an iframe recently loaded from a simple *.txt
file, the code

DIR = Fram.contentDocument.body
DIR = DIR.textContent || DIR.innerText // is latter needed? Yes, IE8
alert(DIR) // for VK

directly shows in the alert window plain text, not preceded by anything
using angle-brackets, for MS IE 8, Firefox 3.0.15, Opera 10.01, Safari
4.0.3, and Chrome 3.0. The <localhost> shown by Opera, and the
JavaScript shown by Safari, are parts of the alerts, not of their
contents.
This way the text you "see" is in effect the content of a single <pre>
element necessarily altered from the "as it is on disc" to be placed
into this tag. For instance all less-than and greater-than signs will
be converted to the corresponding named HTML entities. The fact that
you were getting so far "by using innerHTML ..., a string which agrees
visually with the content of a TXT file" suggests that so far you were
lucky but not having any problematic characters in your .txt files,

"getting by using innerHTML" is not the same as "getting directly as
innerHTML". IIRC, most browsers wrapped with <pre> and one put rather
more at the top. When I was using innerHTML, I easily removed those by
RegExp.



This is OT to the discussed FAQ topic but an interesting problem per
se. I am thinking to move it into separate thread or you may do it
yourself. I have a rather close request for ggNoSpam, in order to give
users an ability to adjust the regexp spam filter even with zero
knowledge of regular expressions. The abstract task description would
be:
"Given an array of strings with the minimum 2 and the maximum 1o
elements, find the shortest common word in these strings. If no such
common character sequence found, then try to find the biggest subset
of strings having a common word".

"word" is understood in regexp terms. To avoid "rush answers" with
common words like "a" or "the" articles let's define that the shortest
common word must be no shorter than 4 characters.

I've changed my mind about whether, for the present, I want to do that.
It would certainly increase efficiency, though perhaps not noticeably.
But doing that and the changes which would necessarily be associated
with it would be an impediment to extending capability in a direction
which may be possible and useful.

If such a thread is started, I'll participate, if anything worth writing
occurs to me.


var AoS = ["aaa bbb ccc ddd ccc bbb ddd", "bbb zzz ggg", "banana"]

var J, A, K, T, Obj = {}, Z = 0

J = AoS.length
while (J--) { T = {} // for each string
A = AoS[J].split(/\W+/) // make array of words
K = A.length ; while (K--) T[A[K]] = 1 // no internal dupes
for (K in T) Obj[K] ? Obj[K]++ : Obj[K] = 1 // ...
// ... if entry exists, increment, else create entry value 1
}

for (K in Obj) if (Obj[K]>Z) { Z = Obj[K] ; Word = [K, Z] }


Then Word[0] appears in Word[1] of the strings, and no word appears in
more than Word[1] of them.

The last line can be amended by making the test >= and complicating what
follows, to list all words of the highest popularity and not just the
first one found.

By using another variable

VERY SLIGHTLY TESTED (uses technique of LINXCHEK).
 
G

Garrett Smith

Dr said:
In comp.lang.javascript message <[email protected]
september.org>, Thu, 29 Oct 2009 22:11:19, Garrett Smith


You are supposed, as FAQ maintainer, to be sustaining something useful
to the ordinary questioners, especially those who are not full-time
professional JavaScript programmers.

However, you appear entirely unable to understand their positions and
points of view. FAQ maintaining is a task for the sympathetic
communicator; not for the nerd.

A lot of the complaints with the FAQ is too verbose, too long.

The FAQ should not be too much of a chore to read. It should be easy
to understand.

Once the document is found, the next step is to do something with that,
right? That is what DOM and Forms section is for.

Things about the document seem more appropriate for "DOM and Forms", not
"window and frames".

Perhaps worth mentioning:-
| The frame must be fully loaded before its content can be accessed.
|
| fwin.document;// the document
| fwin.document.documentElement; // root element.
|
| See the section on DOM and Forms: #domRef

Perhaps worth another entry:-

How can I know when an iframe has loaded?

First, I'd rather edit/shorten the entry on #getWindowSize.

It is a long entry and explains a workaround for older versions of
Opera. It seems worth removing that workaround and its explanation.
That should shorten the entry considerably.

Less is more, here.
 
G

Garrett Smith

Dr said:
In comp.lang.javascript message <53e60b5e-ddec-4b97-aa14-64c31f883159@j1
9g2000yqk.googlegroups.com>, Sat, 31 Oct 2009 10:08:13, VK

[a lot of context]
"getting by using innerHTML" is not the same as "getting directly as
innerHTML". IIRC, most browsers wrapped with <pre> and one put rather
more at the top. When I was using innerHTML, I easily removed those by
RegExp.

You might try reading from the PRE element:

var fdoc = frames[0].document;
var pre = fdoc.getElementsByTagName("pre")[0];
var htmlString = pre.innerHTML;
var textString = (typeof pre.textContent == "string" ?
pre.textContent : pre.innerText);
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
september.org>, Sat, 31 Oct 2009 23:36:41, Garrett Smith
You might try reading from the PRE element:

var fdoc = frames[0].document;
var pre = fdoc.getElementsByTagName("pre")[0];
var htmlString = pre.innerHTML;
var textString = (typeof pre.textContent == "string" ?
pre.textContent : pre.innerText);

As regards what I wanted to do, success is a matter of history. Fram is
the frame :

DIR = Fram.contentDocument.body
DIR = DIR.textContent || DIR.innerText // is latter needed? Yes, IE8

gives the content of the disc file; viewing alert(DIR) shows an exact
match to viewing the file in Notepad. That alert, commented out and
annotated VK, is in <URL:http://www.merlyn.demon.co.uk/linxchek.htm>.

OTOH, I cannot say what happens with non-Windows (indeed, with non-
XPsp3) systems - UNIX or Mac, for example ; and it would be helpful to
be able to give the necessary UNIX or Mac input to generate a suitable
file. Anyone like to try it in Mac or UNIX.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
september.org>, Sat, 31 Oct 2009 19:42:12, Garrett Smith
A lot of the complaints with the FAQ is too verbose, too long.

The FAQ should not be too much of a chore to read. It should be easy
to understand.

Once the document is found, the next step is to do something with that,
right? That is what DOM and Forms section is for.

Things about the document seem more appropriate for "DOM and Forms", not
"window and frames".

For the FAQ to be useful to its intended readership, its Subjects (as
seen at the beginning) must be structured ENTIRELY from the point of
view of the questioner, without any consideration of the structure of
the answers.

Otherwise, you're writing a nerdy document much like the majority of the
big Flamingo book.

You MUST learn how the ordinary FAQ reader will think, when seeking an
answer.


Perhaps worth mentioning:-
| The frame must be fully loaded before its content can be accessed.

I am incompletely convinced of that. When reading by timeout, I thought
I saw signs of gaining access to an only partially-filled links array.
Certainly they might have been deceiving signs; but the point should be
checked in several actual browsers.

If your sentence were strictly true, one could issue a frame load
directly followed by a frame read, and the system would wait until
loaded before executing the read.

But it would be safe to write :-
Access to frame content should not be attempted before the frame is
fully loaded.

How can I know when an iframe has loaded?

Yes. I have success with the Fram.onLoad event in FF Op Sf Cr, but not
IE, where I seem to need to use a timeout. OTOH I suspect IE8 of lying
to me, or of being confused [*].

Flamingo wrote of a readyState property, which might be pollable.

[*] I get reports of many unused anchors in a file not containing those
anchors, in IE.
 
L

Laurent vilday

Dr J R Stockton :
Garrett Smith :
How can I know when an iframe has loaded?

Yes. I have success with the Fram.onLoad event in FF Op Sf Cr, but not
IE, where I seem to need to use a timeout. OTOH I suspect IE8 of lying
to me, or of being confused [*].

Flamingo wrote of a readyState property, which might be pollable.

I have no idea what Flamingo is, and also have no idea if the following
is acceptable for clj.
But here is a "solution" (with no setTimeout nor setInterval involved) I
am using since a long time with no issues (assuming, obviously, there is
no cross domain policy stuff) so far :

var frm = document.createElement('iframe');
var isLoaded = false;

function iframeOnLoad()
{
if ( !isLoaded )
{
alert('iframe content is loaded, ready to play with');
}
isLoaded = true;
}

function iframeOnReadyState()
{
if ( !isLoaded && this.readyState == "complete" )
{
iframeOnLoad();
}
}

frm.onload = iframeOnLoad;
frm.onreadystate = iframeOnReadyState;
frm.src = 'URL';
 
G

Garrett Smith

Dr said:
In comp.lang.javascript message <[email protected]
september.org>, Sat, 31 Oct 2009 19:42:12, Garrett Smith


Would an ASCII diagram for the FRAMES section help?

+--------------------------+
| window |
| * document |
| |
| <iframe src="x.jsp"> |
| +------------+ |
| | window | |
| | * document | |
| | | |
| +------------+ |
| </iframe> |
| |
| |
+--------------------------+

If a screenshot of firebug could be conceptualized in ASCII, that might
be more effective communication.
For the FAQ to be useful to its intended readership, its Subjects (as
seen at the beginning) must be structured ENTIRELY from the point of
view of the questioner, without any consideration of the structure of
the answers.

There is value in structuring the FAQ by section. One can come back to
it, as a reference. It is probably boring, painful reading material,
probably even more so when introduced with "RTFFAQ"

Gerard's recent comments seems to indicate this is a problem.
Otherwise, you're writing a nerdy document much like the majority of the
big Flamingo book.

You MUST learn how the ordinary FAQ reader will think, when seeking an
answer.

Sure. The FAQ is for many readers, though. Ideally informative on the
surface, and a reference for much deeper research.

The FAQ should be accurate. A reader finding information that is
incorrect will discard it.

Improving or removing the worst parts should help improve readership.
I am incompletely convinced of that. When reading by timeout, I thought
I saw signs of gaining access to an only partially-filled links array.
Certainly they might have been deceiving signs; but the point should be
checked in several actual browsers.

If your sentence were strictly true, one could issue a frame load
directly followed by a frame read, and the system would wait until
loaded before executing the read.

But it would be safe to write :-
Access to frame content should not be attempted before the frame is
fully loaded.
Yes, it that is better that saying must. Appending nodes during load
would be a problem (operation aborted). Reading layout information would
also not work so well.
Yes. I have success with the Fram.onLoad event in FF Op Sf Cr, but not
IE,

Not Safari 2.

where I seem to need to use a timeout. OTOH I suspect IE8 of lying
to me, or of being confused [*].
The nonstandard onload attribute seems to work fairly well:

// Not working in Safari 2.
f1.setAttribute("onload", "f1LoadHandler()");
if(typeof f.onload != "function" && f.attachEvent) {
f.attachEvent("onload", iframeLoadHandler);
}

Nonstandard, but onload="" works in Safari 3+ and others.
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>IFRAME onload</title>
</head>
<body>
<iframe id="iFrame"
src="load-content.html"
height="100" width="100"></iframe>
<pre id="m">-</pre>
<script type="text/javascript">
var f = document.getElementById("iFrame");
f.setAttribute("onload", "iframeLoadHandler()");
if(typeof f.onload != "function" && f.attachEvent) {
f.removeAttribute("onload");
f.attachEvent("onload", iframeLoadHandler);
}
function iframeLoadHandler(){
document.getElementById("m").firstChild.data = "iframe loaded";
}
</script>
</body>
</html>
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
september.org>, Mon, 2 Nov 2009 00:54:58, Garrett Smith
Would an ASCII diagram for the FRAMES section help?

No. You seem not yet to have understood that, in a FAQ, the Subject
lines should represent what a questioner is likely to want to ask,
entirely independently of the nature of any possible solutions; and the
section belonging to each Subject line should provide what a questioner
needs to know, in a manner that he can understand and see to represent a
solution that he can use.

If you want to produce a document which, as a document, is structured to
reflect the internal structure of the ECMAScript language and the
browser DOM, then you should call it something other than a FAQ and let
someone more suitable handle the FAQ.
 

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

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top