Forum Moderators: open

Message Too Old, No Replies

hide html from netscape 4

NN4 conditional statement

         

seashell

8:01 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



This seems very simple, but I know very little about javascript and can't get it to work.

I have a simple page that is formatted using CSS, but it doesn't look the way it is supposed to in netscape 4. What I want to do is have separate html code for netscape 4 (using tables, not positioned divs) upon a browser detection and hide the css positioned divs that are to be used by capable browsers. Therefore, the page will still look nice in netscape 4, but will utilize css and validate in css browsers.

In order to validate, the netscape 4 specific code will need to be in an external document. Can I do this with a variable?

Here's what I tried:

<script type="text/javascript">
if (document.layers)
document.write('<div style="display:none;">');
</script>

<div id="container">content</div>

<script type="text/javascript">
if (document.layers)
document.write('</div>');
</script>

It hides it just fine, but doesn't un-hide it so the whole page is blank after the javascript.

Help! : )

garann

8:54 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



It will break your validation, but you might want to hard-code the closing DIV tag. I think a browser doesn't bother to interpret anything whose style is "display:none;". If that's true, the browser wouldn't bother to read your second piece of Javascript. If the </div> is already in the page, the browser should find it and stop ignoring your content. Maybe someone who knows a bit more about how a browser reads a page will weigh in..

jimbeetle

9:10 pm on Dec 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



seashell,

Your best bet is to take a stroll over to the CSS forum [webmasterworld.com]. Those folks have simple techniques on how to present NN4 with it's own style sheet.

In particular, check DrDoc's Making Style Sheets Work in NN4 [webmasterworld.com].

seashell

9:12 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



I think a browser doesn't bother to interpret anything whose style is "display:none;". If that's true, the browser wouldn't bother to read your second piece of Javascript.

If this is true, it will ignore the hardcoded div also because it is included in the same style as the javascript. It still doesn't work, so I'm assuming that's why...

seashell

9:15 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



jimbeetle:

I spend a lot of time over there, trust me! ;)

The only solution to my Netscape problem is serving it up in a table. I've put a lot of effort into getting my design table-less and validating so I don't want to undo all my work. The CSS works in Netscape for the most part, but there are some spacing issues that just can't be fixed without a table.

seashell

9:20 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



garann:
Momentary lapse of reason on that last post. Your solution did work! I have so many divs I lost count! Thanks!

DrDoc

9:21 pm on Dec 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Re: display:none...

Yes, NN4 does not render anything that is set to display none (hence, you can't switch to display:block and expect it to reappear).

An alternative solution is to make the div absolutely positioned (to remove it from the flow) and use visibility:hidden instead.

Yes, the hardcoded closing div should work. However, it will cause the page not to validate (if that's important to you). Also, the orphaned closing div can cause layout problems if nested inside other divs.

garann

9:28 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



If this is true, it will ignore the hardcoded div also because it is included in the same style as the javascript.

No, it shouldn't.. Try the same thing with getElementById in a modern version of Netscape to see what I mean. It should still be keeping count of closing and opened tags, it just shouldn't try to do anything with them until it's out of the element whose display is none.

Anyway, sorry for the bum advice. Would it be possible to just redirect NN4 users, since you're writing seperate content for them already?

<edit>seashell: It worked? I tried it in NN4 (belatedly..) and it didn't work for me. But hey, if you're happy, I'm happy. :)</edit>

seashell

9:43 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



I've been staring at this code too long!

seashell: It worked? I tried it in NN4 (belatedly..) and it didn't work for me.

I looked at it again and I found two extra </div>s and not just one (I was right the first time). Does it work for you if you add another one?

This breaks the validation more than I expected. And since I came up with this scheme to allow me to keep my validating code it kind of defeats the purpose.

I can't redirect them to another page because I intend to duplicate this to all pages when I get it perfected. Having an alternate for every page on my site isn't an option.

DrDoc

9:47 pm on Dec 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So, what about the absolute positioning thing? :)

seashell

10:01 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



But I only want to hide the section of code from NN4... I can't do that with visibility:hidden can I?

I'm using the following code to create the same top banner for NN4 (only this time using tables):

<script type="text/javascript">
if (document.layers)
{document.write("NN4 code");
}
</script>

All I need to do is hide the code that uses CSS (floats with margins which NN4 butchers) from NN4 so the banner doesn't repeat.

P.S. I'm not very good at absolute positioning. I'll tinker with it :0)

DrDoc

10:17 pm on Dec 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Of course you can ;)

<script type="text/javascript">
if (document.layers)
document.write('<div style="position:absolute;visibility:hidden;">');
</script>

<div id="container">content</div>

<script type="text/javascript">
if (document.layers)
document.write('</div>');
</script>

That should do exactly what you want... but without any of the downsides that come with using display:none

seashell

10:27 pm on Dec 31, 2003 (gmt 0)

10+ Year Member



I seems I still need the hardcoded </div> to get this to work. It doesn't seem to be picking it up from the javascript. The validator really doesn't like what I'm trying to do either.

divs galore!

DrDoc

10:42 pm on Dec 31, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm... Then, how about this:

#container {
display: none;
/*/*/
display: block;
/* End */
}

<div id="container">content</div>

moonbather

2:35 am on Jan 1, 2004 (gmt 0)

10+ Year Member




<head>
<script>
<!--
if (window.sidebar) document.write('<link rel="stylesheet" href="netstyle.css" type="text\/css">')
else
if ((document.all) && (!window.sidebar) && (!window.opera)) document.write('<link rel="stylesheet" href="msiestyle.css" type="text\/css">')
else
if (document.layers)
document.write('<link rel="stylesheet" href="nn4style.css" type="text\/css">')
// -->
</script>
</head>

seashell

2:40 pm on Jan 2, 2004 (gmt 0)

10+ Year Member



#container {
display: none;
/*/*/
display: block;
/* End */
}

Works like a dream and validates too! You guys always come through for me!

Thanks!