Solved

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

Posted on 2009-04-04
6
823 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 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
To Patch or not to Patch? That is the question!

Don't get caught out like thousands of others around the world in the recent Ransomware Fiasco!
Discuss..
- Why it's not a good idea to wait before Patching
- Sensible approaches to Patching discussed
- Add your feedback, comments and suggestions

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Suggested Solutions

Title # Comments Views Activity
URGENT- Can't login to Bizportal over VPN 2 58
Mozilla Favorites 10 49
mozilla how to change the bookmark name of a site 6 30
Secure registration PHP 7 43
The most up-to-date version of this article is on my Blog https://iconoun.com/blog/
This article offers some helpful and general tips for safe browsing and online shopping. It offers simple and manageable procedures that help to ensure the safety of one's personal information and the security of any devices.
Google currently has a new report that is in beta and coming soon to Webmaster Tool accounts. This Micro Tutorial will highlight new features for Google Webmaster Tools.
This Micro Tutorial will demonstrate how nuggets on the Web are formatted by using Chrome Developer Tools. These tools would not only view the site's CSS but it can also modify it and save the CSS to use on your own site.
Suggested Courses

710 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