Good Afternoon,
Over the weekend I was trying to write my first DHTML web page, using JavaScript. I wanted to create a page which has links across the top of the document, and when the users clicks on a link it will toggle the visibility property of a <div> tag, making the appropriate layer become ‘visible’ and the others, ‘hidden’.
For my script, I first assigned the <div> tags to variables, and then created an array of those variables. The links called a single function which used a ‘for loop’ to cycle through the layers and change visibility.
The function I wrote was:
//LayerName consists of the <div> associated with that link
//LayerNameArray consists of the array containing the <div>s.
//Hidden and Visible are variables containing the text “hidden” or “visible” respectively.
function ShowLayer(LayerName)
{
for (count=0;count<LayerNameArray.length; count++)
{
if (LayerName==LayerNameArray[count]){
if (document.all){
LayerName.style.visibility = Visible;}
else{
LayerName.visibility = Visible;}}
else{
if (document.all){
LayerName.style.visibility = Hidden;}
else{
LayerName.visibility = Hidden;}
}}
}
}
I was never able to run this function correctly, but I also never received any errors, it was as if nothing happened when I clicked a link. I tried debugging using window.alert calls and discovered the ‘else’ portion of the function (to change the visibility to hidden) was causing the ‘error’. Finally I got fed up and split the function so that if (LayerName!=LayerNameArray[count]) then it would simply call another function to hide the layers. This worked, but I would really like know why the original function was not correct.
Thanks in advance.
This Guy!
Over the weekend I was trying to write my first DHTML web page, using JavaScript. I wanted to create a page which has links across the top of the document, and when the users clicks on a link it will toggle the visibility property of a <div> tag, making the appropriate layer become ‘visible’ and the others, ‘hidden’.
For my script, I first assigned the <div> tags to variables, and then created an array of those variables. The links called a single function which used a ‘for loop’ to cycle through the layers and change visibility.
The function I wrote was:
//LayerName consists of the <div> associated with that link
//LayerNameArray consists of the array containing the <div>s.
//Hidden and Visible are variables containing the text “hidden” or “visible” respectively.
function ShowLayer(LayerName)
{
for (count=0;count<LayerNameArray.length; count++)
{
if (LayerName==LayerNameArray[count]){
if (document.all){
LayerName.style.visibility = Visible;}
else{
LayerName.visibility = Visible;}}
else{
if (document.all){
LayerName.style.visibility = Hidden;}
else{
LayerName.visibility = Hidden;}
}}
}
}
I was never able to run this function correctly, but I also never received any errors, it was as if nothing happened when I clicked a link. I tried debugging using window.alert calls and discovered the ‘else’ portion of the function (to change the visibility to hidden) was causing the ‘error’. Finally I got fed up and split the function so that if (LayerName!=LayerNameArray[count]) then it would simply call another function to hide the layers. This worked, but I would really like know why the original function was not correct.
Thanks in advance.
This Guy!