Discussion:
TXT file into Iframe causes warning
(too old to reply)
Dr J R Stockton
2014-12-25 12:03:53 UTC
Permalink
I am using Firefox with Tools, Web Developer, Web Console (Ctrl-Shift-K)
and this code :-

function RequestData() {
addCallback(Ifr1, "load", SelectReadings)
Ifr1.src = E7form.Fname.value.trim() }

function SelectReadings() { var Lines
removeCallback(Ifr1, "load", SelectReadings)
Lines = Ifr1.contentDocument.body.innerHTML.split(/\n/)
...

The page executing the code, and the file named in FName, are both on my
Windows C: drive, and the file, which is plain seven-bit text and
numbers in a *.TXT file, shows in the iframe pointed to by Ifr1, and
Lines receives the correct contents.

BUT - Web Console gives the Warning "! The character encoding of a
framed document was not declared. The document may appear different if
viewed without the document framing it." which is annoying.

Can, and if so how can, this particular warning be eliminated, by
altering the beginning of the TXT file or by altering the displaying
page/script or both?

BTW, the code works for me in some other browsers, but not in Chrome.



Observation :-

It is very easy to present the results of a set of calculations in a
TextArea, but AFAICS there is no direct provision for printing the
content of a TextArea. It is sufficiently easy to transfer the content
of the TextArea, adding PRE and changing \n to <br>, to an Iframe (and
to get the same result without using the TextArea). In Firefox at
least, a right click in the Iframe enters a direct route to printing out
_only_ the content of the frame. The following code fragment could
provide sufficient clues :-

Ifr2.src = "javascript:'<pre>" + Arr2.join("<br>") + "<\/pre>'"
Ifr2.style.height = Arr2.length*2.7 + "ex"

One can also add HTML formatting into the contents of Arr2.
--
(c) John Stockton, nr London, UK. For Mail, see Home Page. Turnpike, WinXP.
Web <http://www.merlyn.demon.co.uk/> - FAQ-type topics, acronyms, and links.
Command-prompt MiniTrue is useful for viewing/searching/altering files. Free,
DOS/Win/UNIX now 2.0.6; see my <http://www.merlyn.demon.co.uk/pc-links.htm>.
Jukka K. Korpela
2014-12-26 08:00:09 UTC
Permalink
Post by Dr J R Stockton
BUT - Web Console gives the Warning "! The character encoding of a
framed document was not declared. The document may appear different if
viewed without the document framing it." which is annoying.
This issue can be reproduced simply by using, in a local document, an
<iframe> element that refers to a local plain text document, with just
<iframe src=foo.txt></iframe>. So the issue has really nothing to do
with JavaScript as such, and it seems there there is no JavaScript
solution either (browsers recognize contentDocument.characterSet for an
iframe element, but it is not writeable).
Post by Dr J R Stockton
Can, and if so how can, this particular warning be eliminated, by
altering the beginning of the TXT file or by altering the displaying
page/script or both?
It can be eliminated by saving the plain text document as UTF-8 encoded
with BOM. This makes browsers auto-recognize it as UTF-8 encoded even in
the absence of HTTP headers. Of course, any processing of the data then
needs to skip the first three bytes in the file.
--
Yucca, http://www.cs.tut.fi/~jkorpela/
Dr J R Stockton
2014-12-27 20:20:23 UTC
Permalink
Post by Jukka K. Korpela
Post by Dr J R Stockton
BUT - Web Console gives the Warning "! The character encoding of a
framed document was not declared. The document may appear different if
viewed without the document framing it." which is annoying.
This issue can be reproduced simply by using, in a local document, an
<iframe> element that refers to a local plain text document, with just
<iframe src=foo.txt></iframe>. So the issue has really nothing to do
with JavaScript as such, and it seems there there is no JavaScript
solution either (browsers recognize contentDocument.characterSet for an
iframe element, but it is not writeable).
The non-existence of a JavaScript solution, where one might reasonably
be hoped for, must be on-topic in a JavaScript group.
Post by Jukka K. Korpela
Post by Dr J R Stockton
Can, and if so how can, this particular warning be eliminated, by
altering the beginning of the TXT file or by altering the displaying
page/script or both?
It can be eliminated by saving the plain text document as UTF-8 encoded
with BOM. This makes browsers auto-recognize it as UTF-8 encoded even
in the absence of HTTP headers. Of course, any processing of the data
then needs to skip the first three bytes in the file.
Wikipedia has provided what I needed to understand that. My preferred
editor cannot save plain text as anything other than DOS or UNIX. But
it has no objection to inserting the three characters "0xEF, 0xBB, 0xBF"
= '' (which string may not travel well) from their decimal values at
the beginning of the data file. The code reading the iframe ignores all
lines not containing at least three decimal numbers each of at least
five digits, so an initial line containing a BOM with an attribution to
JKK is, with no further effort, not processed.

Thanks.
--
(c) John Stockton, Surrey, UK. ¬@merlyn.demon.co.uk Turnpike v6.05 MIME.
Web <http://www.merlyn.demon.co.uk/> - FAQish topics, acronyms, & links.
Michael Haufe (TNO)
2015-01-02 15:34:07 UTC
Permalink
Post by Dr J R Stockton
The page executing the code, and the file named in FName, are both on my
Windows C: drive, and the file, which is plain seven-bit text and
numbers in a *.TXT file, shows in the iframe pointed to by Ifr1, and
Lines receives the correct contents.
BUT - Web Console gives the Warning "! The character encoding of a
framed document was not declared. The document may appear different if
viewed without the document framing it." which is annoying.
Can, and if so how can, this particular warning be eliminated, by
altering the beginning of the TXT file or by altering the displaying
page/script or both?
From limited testing, the following seems to work:

<!doctype html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title>iframe text load test</title>
</head>
<body>
<iframe id="frmText" src="data:text/plain;charset=utf-8,Hola%20Mundo"></iframe>
</body>
</html>

Loading...