Why doesn't this work in Netscape?

B

Brett

This code is supposed to work in Netscape 4+ and IE 4+. It works fine in IE
but in Netscape 7.2, I get a blank page. Any suggestions?

Thanks,
Brett


<html>
<head>

<script language="JavaScript"><!--
rowArray = new Array();
rowArray[1] = '<form>Row 1: <input type="button" value="Reveal"
onClick="clicked(2)"><\/form>';
rowArray[2] = 'This is some text for row 1';
rowArray[3] = '<form>Row 3: <input type="button" value="Reveal"
onClick="clicked(4)"><\/form>';
rowArray[4] = 'Row 4 when clicked reveals this text';
rowArray[5] = '<form>Row 5: <input type="button" value="Reveal"
onClick="clicked(6)"><\/form>';
rowArray[6] = 'Contents of the last row';

viewArray = new Array();
viewArray[1] = viewArray[3] = viewArray[5] = true;
viewArray[2] = viewArray[4] = viewArray[6] = false;

function refreshTable() {

var output = '<table border="1" width="500">';
for (var i = 1; i <= rowArray.length; i++) {
if (viewArray)
output += '<tr><td>' + rowArray + '<\/td><\/tr>';
}
output += '<\/table>';

if (document.all)
document.all('myTable').innerHTML = output;
else if (document.layers) {
document.layers['myTable'].document.open();
document.layers['myTable'].document.writeln(output);
document.layers['myTable'].document.close();
}
}

function clicked(x) {
viewArray[x] = !viewArray[x];
refreshTable();
}
//--></script>

</head>

<body onLoad="javascript:refreshTable();">


<span id="myTable" style="position:absolute"></span>


</body>
</html>
 
L

Lee

Brett said:
This code is supposed to work in Netscape 4+ and IE 4+. It works fine in IE
but in Netscape 7.2, I get a blank page. Any suggestions?

I suggest that you ignore any code examples that are supposed
to work in Netscape 4 and IE 4.

That code is completely obsolete.
 
B

Brett

Lee said:
Brett said:

I suggest that you ignore any code examples that are supposed
to work in Netscape 4 and IE 4.

That code is completely obsolete.
Ok but why exactly doesn't it work in Netscape 7.2?

Thanks,
Brett
 
F

Fred Oz

Brett wrote:
[snip]
Ok but why exactly doesn't it work in Netscape 7.2?

I'd guess because your logic is that all browsers support either
document.layers or document.all. That would be wrong - e.g. Safari.

Feature detection is great, but only if you use it correctly. Safari
(and I suspect Netscape 7.2) don't support either of the above, they
expect you to use document.getElementById or similar.

Also, explicitly putting content into arrays is time consuming and makes
life difficult, consider the code below (with feature detection for
getElementById). Watch for line wrapping, I can't manually wrap them
because of your code design.

Lastly, this is the craziest way I've seen to hide and show rows.
What's wrong with simply giving each row an id, then hide/show them by
modifying the display attribute? A re-write is below your fixed code -
I think you will find it vastly simpler.

You can also ditch the "<!-- //-->" junk to hide scripts unless you
think someone with Netscape 1.0 or Mosaic will use your page.

Fred.

Partial fix of original code:

<html>
<head>

<script language="JavaScript">
var rowArray = [
'empty',
'<form>Row 1: <input type="button" value="Reveal"
onClick="clicked(2)"><\/form>',
'This is some text for row 1',
'<form>Row 3: <input type="button" value="Reveal"
onClick="clicked(4)"><\/form>',
'Row 4 when clicked reveals this text',
'<form>Row 5: <input type="button" value="Reveal"
onClick="clicked(6)"><\/form>',
'Contents of the last row',
]

viewArray = new Array();
viewArray[1] = viewArray[3] = viewArray[5] = true;
viewArray[2] = viewArray[4] = viewArray[6] = false;

function refreshTable() {

var output = '<table border="1" width="500">';
for (var i = 1; i <= rowArray.length; i++) {
if (viewArray)
output += '<tr><td>' + rowArray + '<\/td><\/tr>';
}
output += '<\/table>';

if (document.all) {
document.all('myTable').innerHTML = output;
alert('Using doc.all');
} else {
if (document.layers) {
alert('Using doc.layers');
document.layers['myTable'].document.open();
document.layers['myTable'].document.writeln(output);
document.layers['myTable'].document.close();
} else {
if (document.getElementById) {
document. getElementById('myTable').innerHTML = output;
alert('Using doc.GEBI\n' + output);
}

}
}
}

function clicked(x) {
viewArray[x] = !viewArray[x];
refreshTable();
}
</script>

</head>

<body onLoad="javascript:refreshTable();">


<span id="myTable" style="position:absolute"></span>


</body>
</html>


Version using element attributes:

<html>
<head><title>Show/Hide Rows</title>

<script type="text/javascript">
function showHide(r) {
if (r.style.display == '') {
r.style.display = 'none';
} else {
r.style.display = '';
}
}
</script>
</head>
<body>
<table>
<tr id="row1">
<td>Here is cell 1
<form action="">
<input type="button" value="show/hide row 2"
onclick="showHide(document.getElementById('row2'))"
</td>
</tr><tr id="row2">
<td>Here is cell 2</td>
</tr>
</table>
</body>
</html>
 
F

Fred Oz

Fred Oz wrote:
[snip]
Version using element attributes:

Aggghhh! I copied a pasted in two chunks, and of course stuffed it up.
The code should work but only becaue of browser tolerance, here is the
corrected code:

<html>
<head><title>Show/Hide Rows</title>

<script type="text/javascript">
function showHide(r) {
if (r.style.display == '') {
r.style.display = 'none';
} else {
r.style.display = '';
}
}
</script>
</head>
<body>
<table>
<tr id="row1">
<td>Here is cell 1
<form action="">
<input type="button" value="show/hide row 2"
onclick="showHide(document.getElementById('row2'))"</form>
</td>
</tr><tr id="row2">
<td>Here is cell 2</td>
</tr>
</table>
</body>
</html>
 
M

Mark Preston

Brett said:
This code is supposed to work in Netscape 4+ and IE 4+.

No it isn't.
function refreshTable() {

[snip]

if (document.all)
document.all('myTable').innerHTML = output;
else if (document.layers) {
document.layers['myTable'].document.open();
document.layers['myTable'].document.writeln(output);
document.layers['myTable'].document.close();
}
}

And that is why it isn't.

"document.all" is for MSIE and "document.layers" is for Netscape 4.
Everything else is a dead duck.
 
R

Randy Webb

Mark said:
No it isn't.

If advertised as "IE4+ and Netscape 4+", then yes, its "supposed" to
work, even if it doesn't work.
function refreshTable() {

[snip]

if (document.all)
document.all('myTable').innerHTML = output;
else if (document.layers) {
document.layers['myTable'].document.open();
document.layers['myTable'].document.writeln(output);
document.layers['myTable'].document.close();
}
}

And that is why it isn't.

"document.all" is for MSIE and "document.layers" is for Netscape 4.

No, and No.

Opera in Spoof mode honors one or the other. Firefox honors
document.all, and there is even a browser (I don't recall the name, just
recall Jim Ley mentioning it and Richard Cornford may be able to name
it) that supports *both* of those.
Everything else is a dead duck.

See above.
 
R

Richard Cornford

Randy said:
Mark Preston wrote:

No, and No.

Opera in Spoof mode honors one or the other. Firefox
honors document.all, and there is even a browser (I
don't recall the name, just recall Jim Ley mentioning
it and Richard Cornford may be able to name it)
that supports *both* of those.
<snip>

It was Omniweb

Richard.
 
M

Mark Preston

Randy said:
Mark Preston wrote:

If advertised as "IE4+ and Netscape 4+", then yes, its "supposed" to
work, even if it doesn't work.
Don't know about you, but I didn't see any claim from the provider, just
from the OP. And as far as I can see it is "supposed" to work in MSIE 4+
and Netscape 4 (not "plus").

Which was what I pointed out, rather more briefly.
No, and No.

Sorry, but "yes and yes".
Opera in Spoof mode honors one or the other. Firefox honors
document.all, and there is even a browser (I don't recall the name, just
recall Jim Ley mentioning it and Richard Cornford may be able to name
it) that supports *both* of those.

I know they do - but that is not what they were set up for. The
"document.all" object was for IE and similarly "document.layers" was for
Netscape 4. It is true, as you said, that other browsers do indeed *use*
those forms, but that is not what they were set up for.

It is always - almost - best to stick to standards and to avoid this
sort of provider-specific code. Granted, in the code referred to it
would have been a lot more difficult to do since both were *extensions*
to the standard that then existed so that additional features could be
used in the specific browsers (and it is because they were extensions
that offered additional features that they were "spoofed" in other
browsers). But that does not, I'm afarid, alter the point that they are
strictly propietary (and in the case of "document.layers" even version
specific) *extensions* to the standard and cannot be relied on.
 
R

Randy Webb

Mark said:
Sorry, but "yes and yes".

The part of my quote that you snipped said this:

"All others are dead ducks"

And that is patently false, hence my "No, and No.", just as much as
saying "document.all is for MSIE" is patently false. To me that says its
for MSIE only, when its not. And for years the assumption by people that
didn't know any different was something along these lines:

if (document.all){
//its IE
}

And that is definitely wrong. While document.all was first implemented
by MSIE and document.layers in NS4, it does not make them for those
browsers alone. Maybe its just the way we interpret what we read *shrug*.
I know they do - but that is not what they were set up for. The
"document.all" object was for IE and similarly "document.layers" was for
Netscape 4. It is true, as you said, that other browsers do indeed *use*
those forms, but that is not what they were set up for.

That is true, and I agree. But to say, now, that document.all is IE only
and document.layers is NS4 only is just plain wrong. Irrelevant of what
they were originally intended for.
It is always - almost - best to stick to standards and to avoid this
sort of provider-specific code.

That depends on the standard and how well it's implemented. But then, I
am not a big fan of "standards" as I am a fan of whats reality.
Standards (with regards to ECMA) is how things *should* be, not how
things *are*. .toFixed() being probably the simplest and easiest to
screw up yet MS did it. So, do you stick to the "standard", or, do you
make it work around the limitation in the flaw of MS' implementation of it?

Granted, in the code referred to it would have been a lot more difficult
to do since both were *extensions* to the standard that then existed so
that additional features could be used in the specific browsers (and it
is because they were extensions that offered additional features that
they were "spoofed" in other browsers).

That code is *very* simple to make work in "modern" browsers using
document.getElementById, and quite trivially at that. Either way, it
doesn't change the simple reality that document.all is not IE4+ and
document.layers is not NS4 (exclusively).
 
M

Mark Preston

Randy said:
"All others are dead ducks"

And that is patently false, hence my "No, and No.", just as much as
saying "document.all is for MSIE" is patently false. To me that says its
for MSIE only, when its not. And for years the assumption by people that
didn't know any different was something along these lines:

I tend to keep answers short and simple. That is (usually) why people
ask short and simple questions. Yes, as I said, others will *use* them
but that it not what they were created *for*, which was what I
addressed. Your assumption is reasonable and true, but is neither what I
said nor what I inteded to say.
That code is *very* simple to make work in "modern" browsers using
document.getElementById, and quite trivially at that.

I agree again.
 

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
474,263
Messages
2,571,062
Members
48,769
Latest member
Clifft

Latest Threads

Top