Solved

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

Posted on 2009-04-04
6
815 Views
Last Modified: 2013-12-08
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

0
Comment
Question by:PhilJMcDougall
  • 5
6 Comments
 
LVL 29

Accepted Solution

by:
Badotz earned 500 total points
ID: 24202942
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
 
LVL 29

Expert Comment

by:Badotz
ID: 24202952
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
 
LVL 1

Author Comment

by:PhilJMcDougall
ID: 24203098
will give it a go later and let you know :o) cheers for getting back to me!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 29

Expert Comment

by:Badotz
ID: 24203127
Hey, its part of the plan!
0
 
LVL 29

Expert Comment

by:Badotz
ID: 24209253
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
 
LVL 29

Expert Comment

by:Badotz
ID: 24222225
Is this still an issue?
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This Micro Tutorial will demonstrate how to add subdomains to your content reports. This can be very importing in having a site with multiple subdomains.
How to create a custom search shortcut to site-search Experts Exchange using Google in the Firefox browser. This eliminates the need to type out site:experts-exchange.com whenever you want to search the site. Launch your Bookmark Menu: Press 'Ctrl +…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question