/    Sign up×
Community /Pin to ProfileBookmark

Script making navigation bar dissappear!

Ok, I’m a newbie and trying to build this navigation bar that works with onClick which reveals horizontal sub navigation. I apply this javascript and the nav bar dissappears. You can see it at the link below. If you hit refresh you’ll be a bale to see the nav bar for a split of second.

Have a look: [url]http://auctionexcel.com/css/test5.htm[/url]

Here’s the code:

<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN”
http://www.w3.org/TR/html4/loose.dtd“>
<html>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
<title>Untitled Document</title>
<script type=”text/javascript”>
function init()
{
if(document.getElementById && document.createTextNode)
{
var mn=document.getElementById(‘mainnav’);
var as=mn.getElementsByTagName(‘a’);
for (var i=0;i<as.length;i++)
{
as[i].onclick=function(){show(this);return false}
as[i].onkeypress=function(){show(this);return false}
}
hidem();
}
}
function show(l)
{
hidem();
var id=l.href.match(/#(w.+)/)[1];
document.getElementById(id).style.display=’block’;
}
function hidem()
{
for (var i=0;i<document.getElementsByTagName(‘div’).length;i++)
{
document.getElementsByTagName(‘div’)[i].style.display=’none’;
}
}
window.onload=init;
</script>
<style type=”text/css”>
/* navigation */
#nav {
float: left;
margin: 0;
padding: 0;
list-style: none;
font-size: 75%;
font-family:Arial, Helvetica, sans-serif;
background:url([url]http://auctionexcel.com/css/images/navcorner_right.gif[/url]) no-repeat right top;
padding-right:7px;
height:33px;
}
#nav ul {
margin: 0px;
padding: 0px;
padding-left:7px;
height:33px;
background:url([url]http://auctionexcel.com/css/images/navcorner_left.gif[/url]) no-repeat left top;
float:left;
}
#nav li {
float: left;
margin: 0;
padding: 0;
list-style: none;
background: url(images/navback_darkgreen.gif);
height:33px;
line-height:33px;
}
#nav a {
float: left;
padding:0 16px;
height:33px;
text-decoration: none;
border-bottom: none;
color: #fff;
background: url(images/navback_lightgreen.gif);
}
#nav a.last{padding:0 18px;}
#nav a:hover, #nav a.active {
color: #fff;
background:transparent;
}

</style>
</head>
<body>
<div id=”nav”>
<ul id=”mainnav”>
<li><a href=”#acc”>Accounts</a></li>
<li><a href=”#mor”>Mortgages</a></li>
<li><a href=”#loa”>Loans</a></li>
<li><a href=”#con”>Convenience Services</a></li>
<li><a href=”#inv”>Investment Planning</a></li>
<li><a href=”#you”>Young Finance</a></li>
<li><a class=”last” href=”#”>FMFCU Info</a></li>
</ul>
</div>
<div id=”acc”>…why data…</div>
<div id=”mor”>…now data…</div>
<div id=”loa”>…brown data…</div>
<div id=”con”>…cow data…</div>
</body>
</html>

to post a comment
JavaScript

11 Comments(s)

Copy linkTweet thisAlerts:
@jamFeb 28.2006 — im not sure what the problem is but.... i'd guess its a script problem.... if the syntax is wrong or anything is left unclosed the page will not be shown prperly
Copy linkTweet thisAlerts:
@jamFeb 28.2006 — oh... i've had a look at the script and i'd guess that the problem is in the bottom... the actual html..... in the script you have a getElementByTagName('div')... but all your html is div tags.......


[COLOR=Red]<div id="nav">[/COLOR]

<ul id="mainnav">

<li><a href="#acc">Accounts</a></li>

<li><a href="#mor">Mortgages</a></li>

<li><a href="#loa">Loans</a></li>

<li><a href="#con">Convenience Services</a></li>

<li><a href="#inv">Investment Planning</a></li>

<li><a href="#you">Young Finance</a></li>

<li><a class="last" href="#">FMFCU Info</a></li>

</ul>

[COLOR=Red]</div>[/COLOR]

[COLOR=Red]<div id="acc">...why data...</div>

<div id="mor">...now data...</div>

<div id="loa">...brown data...</div>

<div id="con">...cow data...</div>[/COLOR]


see if everything is in a div tag and your script is sketchy..... it doesnt work..... so thats the first problem....the second is that your script chooses not to display the div tags onLoad... i dont know why but..... i've done all i can


JAM
Copy linkTweet thisAlerts:
@Gazza123Mar 01.2006 — Hello AAM,

A fresh pair of eyes maybe...

Remove the hidem() call in your init() function.

Gary
Copy linkTweet thisAlerts:
@AAMauthorMar 01.2006 — The problem seems to be in the one div tag which when removed eliminates the problem.

[COLOR=Red]<div id="nav">[/COLOR]

<ul id="mainnav">

<li><a href="#acc">Accounts</a></li>

<li><a href="#mor">Mortgages</a></li>

<li><a href="#loa">Loans</a></li>

<li><a href="#con">Convenience Services</a></li>

<li><a href="#inv">Investment Planning</a></li>

<li><a href="#you">Young Finance</a></li>

<li><a class="last" href="#">FMFCU Info</a></li>

</ul>

[COLOR=Red]</div>[/COLOR]

Does this give any clues??
Copy linkTweet thisAlerts:
@konithomimoMar 01.2006 — The problem is the call to hidem() at the end of init(). The reason why it works once you remove the div tag is because hidem() looks for all divs, so if you remove the div tag then it wont find that bit of code.
Copy linkTweet thisAlerts:
@AAMauthorMar 01.2006 — I kinda get what you mean. I'm not there yet to make the necessary changes in the javascript code to make it work and removing the div will spoil the look of the nav bar.

Can't we make the code to work with specific divs?
Copy linkTweet thisAlerts:
@konithomimoMar 01.2006 — replace your function hidem() with this one:
function hidem()
{var divs = document.getElementsByTagName('div');
for (var i=0;i&lt;divs.length;i++)
{
if(divs[i].id!="nav")
divs[i].style.display='none';
}
}


now it will affect every div except for the nav one.
Copy linkTweet thisAlerts:
@AAMauthorMar 01.2006 — That worked. but is see one potential pitfall with this. web pages may have many other divs in them! so is there way to reverse the script so that only a particular div or divs are hidden and the others are shown??
Copy linkTweet thisAlerts:
@konithomimoMar 02.2006 — function hidem()
{var divs = document.getElementsByTagName('div');
var show = new Array('nav');
for (var i=0;i&lt;divs.length;i++)
{
for(var j=0;j&lt;show.length;j++)
{
if(divs[i].id==show[j])
break;
if(j==show.length-1)
divs[i].style.display='none';
}
}
}


Just add to the array show, with the IDs of the divs that you don't want to hide. For now the only name in there is 'nav'. To add more names just insert in a comma and then the name in quotation marks. For example, if you had a div called menu and you didnt want to hide it then your array would be:

var show = new Array('nav','menu');
Copy linkTweet thisAlerts:
@AAMauthorMar 03.2006 — really appreciate your help, but with hundreds of pages in the site wouldn't it be counter productive to have to write down names of all divs in all pages that i don't want to hide?

I'm sure there must be a way to reverse the the way it's coded so that we identify what needs to be hidden and the remaining divs show by default.
Copy linkTweet thisAlerts:
@konithomimoMar 03.2006 — give your divs classes and then hide/show whatever classes you want. That is the easiest way.
×

Success!

Help @AAM spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...