...

View Full Version : Layer visibility, help, I give up. I cant see what i am doing wrong.



Soldier Bob
07-08-2002, 08:00 PM
I am trying to get this to work on MS IE 5.00.xxxx
It should take in a layer, make it invisible or visible.




function toggle_layer_visibility( layer_to_change ) {

if ((layer_to_change != null) && (layer_to_change != "")) {
if (document.layers) {
alert("Debug: netscape");
if (document.layers[layer_to_change].visibility == 'visible' || document.layers[layer_to_change].visibility == 'show') {
alert("Debug: layer is currently shown. Attempting to hide:");
document.layers[layer_to_change].visibility = 'hidden';
} else if (document.layers[layer_to_change].visibility == 'hidden' || document.layers[layer_to_change].visibility == 'hide') {
alert("Debug: layer is currently hidden. Attempting to show:");
document.layers[layer_to_change].visibility = 'visible';
} else {
}

} else if (document.all) {
alert("Debug: IE");
if (document.all[layer_to_change].style) {
alert("Debug: a style attribute exists for the " + layer_to_change);
}
alert("Debug: It is: " + document.all[layer_to_change].style.visibility);
if (document.all[layer_to_change].style.visibility) {
alert("Debug: a visibility attribute exists for the " + layer_to_change + ". It is: " + document.all[layer_to_change].style.visibility);
}
if (document.all[layer_to_change].style.visibility == 'visible') {
alert("Debug: layer is currently shown. Attempting to hide:");
document.all[layer_to_change].style.visibility = 'hidden';
} else if (document.all[layer_to_change].style.visibility == 'hidden') {
alert("Debug: layer is currently hidden. Attempting to show:");
document.all[layer_to_change].style.visibility = 'visible';
//.style.visibility
}
}

if (document.getElementById) {
alert("Debug: " + document.getElementById(layer_to_change).style.visibility );
} else {
alert("Debug: getElementById does not exist.");
}


} else {
alert('Nothing was sent to the layer_toggle_visibility function');
}
}

ACJavascript
07-08-2002, 08:06 PM
inseat of putting if(document.all[layer_to_change].style.visibility="visible or hidden"

try this:

var IEstyle=eval("document.all."+layer_to_change+".style.")

if(IEstyle.visibility="visible"){


ect.

Mabye it'll work, ya never know hehe ;D;D




Hope this helps:D:D

adios
07-08-2002, 08:18 PM
Bob...

This is where the HTMLElement.style property comes from:

<div.........style=".........">

It represents (in the DOM object hierarchy) the inline style attribute - from HTML. It's not a 'magic clearing house' of CSS, so unless you assign that element's CSS inline - as opposed to in a global style sheet - its .style object will remain a collection of empty strings. There are ways of 'getting computed style' (checking the entire cascade to determine the actual CSS implemented), and newer CSS platforms will have this ability built in. Till then, might try something like this:



function getElem(id) {
return document.all ? document.all(id) :
document.getElementById ? document.getElementById(id) :
document.layers ? document.layers[id] :
null;
}

function getVisibility(el) {
if (el.style) {
if (el.style.visibility && el.style.visibility != 'inherit') return el.style.visibility;
if (el.currentStyle && el.currentStyle.visibility && el.currentStyle.visibility != 'inherit')
return el.currentStyle.visibility;
else while (el = el.parentElement) {
if (el != null && el.style.visibility && el.style.visibility != 'inherit')
return el.style.visibility;
if (el != null && el.currentStyle && el.currentStyle.visibility &&
el.currentStyle.visibility != 'inherit')
return el.currentStyle.visibility;
}
} else if (document.layers) {
if (el.visibility != 'inherit') return el.visibility;
else while (el = el.parentLayer) {
if (el != null && el.visibility != 'inherit') {
return el.visibility;
}
}
}
return (!document.layers) ? 'visible' : 'show';
}

function toggleVis() {
var currArg;
for (var a=0; a<arguments.length; ++a) {
currArg = arguments[a];
var el = getElem(currArg);
if (!el) return;
if (el.style)
el.style.visibility = (getVisibility(el) == 'visible') ? 'hidden' : 'visible';
else if (document.layers)
el.visibility = (getVisibility(el) == 'show') ? 'hide' : 'show';
}
}


toggleVis() takes multiple arguments (element ids).

Soldier Bob
07-08-2002, 09:03 PM
dih,

>Bob...
> This is where the HTMLElement.style property comes from:
><div.........style=".........">

Thx. Adios: I had just come to that conclusion that it wasnt declared until you declare it... Programming for Netscape 4.7x + got my mind into one track thinking.

OMG... That code is great... too bad my brain cant digest it at this time. I think I will just add the following code to the page load up script. I dont want to have to explain your code to anyone who works on this after me, just yet.



if (document.all) {
my_layer.style.visibility = "visible";
}



-S. Bob

"And they invented javascript, and all was in chaos again"

adios
07-08-2002, 09:28 PM
%%%%
% :eek: % <-- bob
%%%%

Quick explanation: elements inherit their CSS if it isn't applied to them directly; so, find your way up through the tree (old way), checking for set visibility, returning if found, continuing if not or if it's set to 'inherit'. If you hit the end (document element) and haven't found it, it must be the default visibility, which is, naturally, visible. The .currentStyle object is IE-only, returns the cascaded style, usually.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum