Solved

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

Posted on 2009-04-04
6
785 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
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…
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.

708 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now