Forum Moderators: open
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">
<title>Only one line is output in IE8</title>
</head>
<body>
<img src="img/one.jpg" width="100" height="75" alt="1st Demo Image">
<img src="img/two.jpg" width="100" height="75" alt="2nd Demo Image">
<img src="img/three.jpg" width="100" height="75" alt="3rd Demo Image">
<img src="img/four.jpg" width="100" height="75" alt="4th Demo Image">
<pre><script type="text/javascript">
// Only the first line is output in IE8, other browsers OK
// Unless you include additional white-space directly inside the <pre> element!
for (var n=0; n<document.images.length; n++) {
document.writeln(n + ' - ' + document.images[n]['width']);
}
</script></pre>
</body>
</html>
...can you work out a way where this will function properly in IE8 and write an if/then to detect the browser and run the alternate code for IE8 users?
document.writeln()outside of the for loop solves the problem...
for (var n=0; n<document.images.length; n++) {
document.writeln(n + ' - ' + document.images[n]['width']);
} var htm = [];
for (var n=0; n<document.images.length; n++) {
htm.push(n + ' - ' + document.images[n]['width']);
}
document.write(htm.join('\n'));
preand 9.1 of the html recs expressly exempt pre from the expectation visual user agents will "collapse" white-space that is "unnecessary". So any whitespace/newline inside <pre> should produce the results described - although note that per 9.3.4 of the html recs the characteristics of <pre> are not mandatory.
This value prevents user agents from collapsing sequences of white space. Lines are only broken at preserved newline characters.
...document.writeIn should generate a new line at the end of each statement. Best I can tell, ie8 is recognising some character at the end of each expected "line", but not translating/acting on that as a newline.
document.write()as well, when no newlines are output. Also (another side issue), IE7 appears to have trouble with writeln(), in that it treats it exactly the same as write(), without newlines! incidentally, that should be the only difference between write() and writeln() - the convenience of a newline appended to the end of the output.
I can't see newline characters in the code though - which could be a clue to the javascript issue.
writeln().
Couldn't reproduce the issue with block elements such as <p>.
<pre><script>...</script></pre>
<pre><samp><script>...</script></samp></pre>
display:block(This is actually how I found the problem in the first place.)
<pre><samp style="display:block;"><script>...</script></samp></pre>
<samp style="display:block;">with a <p> and this also causes the problem again. Although this is invalid HTML and so should perhaps be discounted.)
In terms of separating the <pre> and <script> elements by a space (or newline), and seeing that reflected in the formatting, I believe that is correct.
What fascinated me is that clicking on the first line expands the element to display all the output, formatted exactly as desired!