Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2009-04-04
6
Medium Priority
?
839 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
6 Comments
 
LVL 29

Accepted Solution

by:
Badotz earned 2000 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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

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

SSL stands for “Secure Sockets Layer” and an SSL certificate is a critical component to keeping your website safe, secured, and compliant. Any ecommerce website must have an SSL certificate to ensure the safe handling of sensitive information like…
This article discusses how to implement server side field validation and display customized error messages to the client.
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.
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

722 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