show/hide layers onLoad

G

geotso

Hi

I'm looking for a javascript with the following operation:

if history.length <=2
show "layer1" and hide "layer2"

else
hide "layer1' and show "layer2"

Notes
"layer1" and "layer2" are the IDs of two layers
this should be in a function, in order to call it with onLoad

Can someone help me please?
 
S

Stephen Chalmers

geotso said:
Hi

I'm looking for a javascript with the following operation:

if history.length <=2
show "layer1" and hide "layer2"

else
hide "layer1' and show "layer2"

Notes
"layer1" and "layer2" are the IDs of two layers
this should be in a function, in order to call it with onLoad

Can someone help me please?

history.length is treated inconsistently across browsers.

Assuming both layers are initially styled as hidden.

Call with: <body onload='showLayer("layer1","layer2")'>

function showLayer(L1, L2)
{
var ref=null;
if( document.getElementById && (ref=document.get
ElementById(history.length<=2 ? L1 : L2)) )
ref.style.visibility='visible';
}
 
G

geotso

Stephen said:
history.length is treated inconsistently across browsers.

Assuming both layers are initially styled as hidden.

Call with: <body onload='showLayer("layer1","layer2")'>

function showLayer(L1, L2)
{
var ref=null;
if( document.getElementById && (ref=document.get
ElementById(history.length<=2 ? L1 : L2)) )
ref.style.visibility='visible';
}

It works like a charm!

(a small change, just for the records:
I've used style="display:none" and then .style.display=''; instead of
style="visibility:hidden" and .style.visibility='visible'.
This way, I prevent browser's window from displaying the vertical scrollbar
if anyone of the layers is too short to need it).

Thank you very much!!
 

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
473,733
Messages
2,569,439
Members
44,829
Latest member
PIXThurman

Latest Threads

Top