Javascript not working in IE but works in Firefox, Chrome etc.

Hi Experts,

Im currently developing a music site with some javascript in the navigation. When you click on Say News, Band, Tour etc the sub links appear below.... Then if you clicked on the next one the previous one closes... its basically a Javascript Show/Hide script.

It works on Chrome & Firefox but not on IE.... obviously its important that it works on i.e. as the site is for one of the worlds largest bands soo its pretty important that i resolve this quickly.


<script language="JavaScript">
//here you place the ids of every element you want.
var ids=new Array('news','band','tour','music','media');
 
function switchid(id){	
	hideallids();
	showdiv(id);
}
 
function hideallids(){
	//loop through the array and hide each element by id
	for (var i=0;i<ids.length;i++){
		hidediv(ids[i]);
	}		  
}
 
function hidediv(id) {
	//safe function to hide an element with a specified id
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'none';
		}
		else { // IE 4
			document.all.id.style.display = 'none';
		}
	}
}
 
function showdiv(id) {
	//safe function to show an element with a specified id
		  
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(id).style.display = 'block';
	}
	else {
		if (document.layers) { // Netscape 4
			document.id.display = 'block';
		}
		else { // IE 4
			document.all.id.style.display = 'block';
		}
	}
}
</script>
 
HTML Code:
 
<td height="27"><a href="javascript:switchid('news');" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('News','','images/images/leftnav_ov__02.png',1)"><img src="images/images/leftnav_02.png" alt="News" name="News" width="171" height="27" border="0" id="News" /></a><div id='news' style="display:none;" class="content2" align="right"><a href="news.php">News</a><img src="images/spacer.gif" width="5" height="1"><br><a href="archive.php">Archived</a><img src="images/spacer.gif" width="5" height="1"></div></td>

Open in new window

LVL 1
PhilJMcDougallAsked:
Who is Participating?
 
BadotzConnect With a Mentor Commented:
I created a page to test this (see the snippet below).

Changes:
  o the incorrect "img" [id] of 'News' is now 'news' (the reason IE failed)
  o ditto for the [name] attribute (for consistency)
  o added logic to test for the existence of an object before showing/hiding (IE whined here, too)
  o the <script> declaration
  o the "ids" variable declaration
  o closed HTML elements that were not closed

I changed the URL for the images to those I had available, but you should be able to change them back. I saw that your images did not all have the same path - some were in 'images/images/', others in 'images/' and still others had no path. Make sure this is correct.

Let me know if this fixes your problems.


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>Big Band Page</title>
<script type="text/javascript">
//here you place the ids of every element you want.
var ids = ['news','band','tour','music','media'];
 
function switchid(id){  
        hideallids();
        showdiv(id);
}
 
function hideallids(){
        //loop through the array and hide each element by id
        for (var i = 0; i < ids.length; i++) {
                hidediv(ids[i]);
        }                 
}
 
function hidediv(id) {
        //safe function to hide an element with a specified id
        if (document.getElementById) { // DOM3 = IE5, NS6
            if (document.getElementById(id)) {
				document.getElementById(id).style.display = 'none';
            }
        }
        else {
                if (document.layers) { // Netscape 4
					if (document.id) {
                        document.id.display = 'none';
                    }
                }
                else { // IE 4
					if (document.all.id) {
                        document.all.id.style.display = 'none';
                    }
                }
        }
}
 
function showdiv(id) {
        //safe function to show an element with a specified id
                  
        if (document.getElementById) { // DOM3 = IE5, NS6
            if (document.getElementById(id)) {
                document.getElementById(id).style.display = 'block';
            }
        }
        else {
                if (document.layers) { // Netscape 4
					if (document.id) {
                        document.id.display = 'block';
                    }
                }
                else { // IE 4
					if (document.all.id) {
                        document.all.id.style.display = 'block';
                    }
                }
        }
}
 
function MM_swapImgRestore() {
	document.getElementById('news').src = 'images/images/googlelogo.jpg';
}
 
function MM_swapImage(id, blank, image, flag) {
	document.getElementById(id).src = image;
}
 
</script>
</head>
<body>
<table id="big_band">
<tbody>
<tr>
<td height="27">
<a href="javascript:switchid('news');" onMouseOut="MM_swapImgRestore()" onMouseOver="MM_swapImage('news','','images/images/left_bg.jpg',1)">
<img src="images/images/googlelogo.jpg" alt="News" name="news" width="171" height="27" border="0" id="news" />
</a>
<div id='Div1' style="display:none;" class="content2" align="right">
<a href="#">News</a>
<img src="images/images/left_bg.jpg" alt="" width="5" height="1" />
<br>
<a href="#">Archived</a>
<img src="images/images/right_bg.jpg" alt="" width="5" height="1" />
</div>
</td>
</tr>
</tbody>
</table>
</body>
</html>

Open in new window

0
 
BadotzCommented:
I tested this in IE 8 and Firefox 3.0.8 - both seemed to work.

Not knowing what the two "MM" function looked like, I made some assumptions for expediency.
0
 
PhilJMcDougallAuthor Commented:
will give it a go later and let you know :o) cheers for getting back to me!
0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
BadotzCommented:
Hey, its part of the plan!
0
 
BadotzCommented:
Follow-up:

I noticed that there is an <img> tag with the [id] of "News" and a <div> tag with an [id] of "news". I would consider these to be unique (but fraught with peril) but IE does not - so I changed the <img> tag [id] to "img_news". That made a BIG difference.

Sorry I didn't see that earlier...
0
 
BadotzCommented:
Is this still an issue?
0
All Courses

From novice to tech pro — start learning today.