View Full Version : layers are not hidding as they should ??
chris_angell
09-30-2002, 11:36 AM
function swap(showobj,showobj,hideobj,hideobj,hideobj) {
shownon(showobj)
hidenon(hideobj)
}
function shownon(showobj) {
if (ns4) document.layers[showobj].visibility = "show"
else if (ie4) document.all[showobj].style.visibility = "visible"
}
function hidenon(hideobj) {
if (ns4) document.layers[hideobj].visibility = "hide"
else if (ie4) document.all[hideobj].style.visibility = "hidden"
}
this is my body below here ????
onclick="swap('myskillsLyr','2Lyr','1Lyr','portfolioLyr','contactLyr')
mordred
09-30-2002, 12:45 PM
It's really not a good idea to name the parameters all the same... JavaScript is not strongly typed, and functions don't have signatures, so you have to take care not to accidentally use the same name for a variable/parameter again.
Aside from that, your error description is very vague, but if you wanted to say that this sample code does not work in Mozilla or NS6/7, you should be aware that the introduction of the w3c DOM was more than two years ago, so you better adapt your code for standards-compatible browsers (hint: document.getElementById() is a method that provides you with the same functionality as document.all[]).
chris_angell
09-30-2002, 01:57 PM
thank you, but you have to remember that some of us are at different coding levels.. I have only been coding for a couple of months. it takes time to learn ... I do it in my spare time...
thank you for your help. anyway mordred ?
sorry for my very vague description, all I am trying to do is show two divs and hide three divisions, is there an easier way to do this,
there is no error code, it just doesn't work ????
:(
mordred
09-30-2002, 03:56 PM
Oops, I did not want to sound like lecturing, it was just hard to tell from your problem description where your source of confusion is. As I said: If you name the parameters the same, JavaScript can not distinguish between each of them. It only recognizes two values in the function, not five. That's why it tries to show 'mySillsLyr' and hide 'contactLyr', and the rest is left untouched.
As a quick hack, you could try to call the hiding/showing function separately for each parameter:
function swap(showobj1, showobj2, hideobj1, hideobj2, hideobj3) {
shownon(showobj1);
shownon(showobj2);
hidenon(hideobj1);
hidenon(hideobj2);
hidenon(hideobj3);
}
To me this application design still looks somewhat unflexible, it could be further refined by modifying the hide/show function in a way that they accept a variable number of parameters and try to show/hide each layer identified by the parameter (e.g. looping through the arguments array).
Does that help better? :)
adios
09-30-2002, 07:28 PM
To me this application design still looks somewhat unflexible, it could be further refined by modifying the hide/show function in a way that they accept a variable number of parameters and try to show/hide each layer identified by the parameter (e.g. looping through the arguments array). :eek:
function getElement(id) {
return document.getElementById ? document.getElementById(id) :
document.all ? document.all(id) :
document.layers ? document.layers[id] : null;
}
function hideLayer() {
for (var el, a=0; arguments[a]; ++a) {
el = getElement(arguments[a]);
if (el) {
if (el.style) el.style.visibility = 'hidden';
else if (typeof el.visibility != 'undefined') el.visibility = 'hide';
}
}
}
function showLayer() {
for (var el, a=0; arguments[a]; ++a) {
el = getElement(arguments[a]);
if (el) {
if (el.style) el.style.visibility = 'visible';
else if (typeof el.visibility != 'undefined') el.visibility = 'show';
}
}
}
onclick="showLayer('myskillsLyr','2Lyr');
hideLayer('1Lyr','portfolioLyr','contactLyr');"
chris_angell
09-30-2002, 07:47 PM
that is brilliant, I was just thinking, the code I was using was a bit bad..
thanks :thumbsup:
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.