Forum Moderators: open

Message Too Old, No Replies

hide show in ns and ie 4

         

andre73

12:32 pm on Dec 1, 2004 (gmt 0)

10+ Year Member



Hi,
I have a piece of code which does a very good job of hiding and showing a layer in IE6 FF1.0 and other modern browsers. I wonder if there is a way hide and show layers in not only the modern browsers but the old ones as well, NS4 and IE4. I like the way this function receives the id of the layer it is supposed to hide or show. also I like the way the other content on the page behaves when the layer is hidden or shown.
Here is the code I have:

<html>
<head>
<script language="JavaScript1.2">

function hideshow(which){
if (!document.getElementById)
return
if (which.style.display=="none")
which.style.display=""
else
which.style.display="none"
}
</script>

<title>show hide</title>
</head>
<body bgcolor="#D4D0C8">
<table cellpadding="0" cellspacing="0" width="40%" bgcolor="#FFFFFF">
<tr>
<td>
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<th align="left">Lorem</th>
<th align="right" width="40">
<a href="javascript:hideshow(document.getElementById('newsDiv'))">hide/show</a>
</th>
</tr>
</table>
<div id="newsDiv" style="font:24px bold;">
<table id="newsContent" NOWRAP align="center" width=100% cellspacing="0">
<tr>
<td class="padding7-5-7-7">
<table border="0" cellpadding="2" cellspacing="2" width="100%">
<tr>
<td align="right" width=100%><a href="javascript:void(0)" onClick="openWind('0');">Ipsum</a></td>
</tr>
</table>

<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td align="left"><span class="newsDate">Dolor</span></td>
</tr>
<tr>
<td align="left" class="lineUnder"> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. <br> </td>
</tr>
</table>
</td>
</tr>
</table></div>
</td>
</tr>
</table>
<p> Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. </p>
</body>
</html>

Bernard Marx

2:16 pm on Dec 1, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Possible, but a few things first. With respect to DIVs, NS 4...

1. Can only reference those with "position: absolute";
- "relative" at [0,0] may do.
- only inline style too (I think)

2. To reference a positioned div, nested in a positioned div is more complicated. Not covered here.

3. I believe NS4 doesn't render dynamic changes in display.

So, due to #3, I'll go with visibility instead.

Plug-in

First we need a referencing plug-in to teach NS4 and IE4 to understand. This is the best way, since it only needs to be done once in a single, but quite long, statement.

This plug-in is not a full getElementById emulation, because:
- nested layers in NS4 (above)
- will return named elements in IE4, and collections if there are >1.
So don't use an id more than once.

togVisibility

NS4 elements don't have a style object. The style property is accessed directly off the element. A quick test for the existence of the element's style object, with the default returning the element itself..

var style = elm.style¦¦elm;

Then we have to take into account the fact that NS4 has it's own version of the visibility properties ["show","hide"]. Only need worry about "hide", because the you have set it up sensibly so we don't explicitly say "visible¦show".


//---- referencing plug-in --------------------------------------

document.getElementById =
document.getElementById ¦¦
document.all ¦¦
function(id){return this.layers[id]};

//---------------------------------------------------------------

function togVisibility(elm)
{
var style = elm.style¦¦elm;
var hidden = {hidden:1,hide:1}[style.visibility];
if(hidden)
style.visibility = "";
else
style.visibility = document.layers? "hide":"hidden";
}

I don't have NS4 here to test this version. It may need a tweak.
Definitely swap the ¦ symbol in the code for a real pipe.

andre73

9:25 pm on Dec 1, 2004 (gmt 0)

10+ Year Member



thank you for the help.
this did not work for me in netscape 4.79 and in IE6 I was able to toggle the visibility of the text but the white background was still there. However, I've decided to skip NS4 compatibility and just use Iframes instead, it just is not worth the hassle of beeing compatible with those old browsers. Thanks anyway