/    Sign up×
Community /Pin to ProfileBookmark

js menu/submenu–need default image on page load

The following is a distilled version of code I’m using for a menu. It works fine, but I’d like to have a default text and image appear in the submenu column when the page loads. This image-text combo should disappear with the first mouseover, and it doesn’t need to reappear again until the page is reloaded. Is there a simple solution?

Thanks in advance to anyone who can help.

Cheers, Paul Otteson

Here’s the link: [url]http://www.esf.edu/aec/defaultcss2.htm[/url]
—————–

<html>

<head><title></title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>

<style type=”text/css”>
#menu { width: 400px; }
#menu ul { text-align: right; list-style-type: none; width: 130px; border-right: 1px solid #669 }
#menu li { padding-right: 10px; }
#menu li ul { padding-left: 10px; display: none; position: absolute; top: 0; left: 170px; border: none }
#menu li.hover ul { text-align: left; display: block; }
</style>

<script>
window.onload = function() {
var cc = document.getElementById(“submenu”).childNodes;
for(var i = 0; i < cc.length; i++) {
if(cc[i].nodeName && cc[i].nodeName.toLowerCase() == “li”) {
cc[i].onmouseover = function() {
if(window.cmenu && window.cmenu != this)
window.cmenu.className = “”;
(window.cmenu = this).className = “hover”
}
}
}
}
</script>
</head>
<body>
<div id=”menu” >
<ul id=”submenu” style=”position:relative”>
<li><a href=””><b>Menu1</b></a>
<ul>
<li><a href=””>Sub1.1</a>
<li><a href=””>Sub1.2</a>
<li><a href=””>Sub1.3</a>
</ul>
</li>
<li><a href=””><b>Menu2</b></a>
<ul>
<li><a href=””>Sub2.1</a>
<li><a href=””>Sub2.2</a>
</ul>
</li>
</ul>
</div>
</body>
</html>

to post a comment
JavaScript

8 Comments(s)

Copy linkTweet thisAlerts:
@phpnoviceJun 08.2005 — I'd like to have a default text and image appear in the submenu column when the page loads.[/QUOTE]
Meaning what ... precisely?
Copy linkTweet thisAlerts:
@ottoworksauthorJun 08.2005 — Thanks. First, Please note that I'm a javascript cut-and-paste hack who can't write a script from scratch.

When this page opens -- http://www.esf.edu/aec/defaultcss.htm -- the right (submenu) column is empty. Mouseover the left column and submenus appear.

I would like to have content appear in the right column when the page opens, as it does on this page -- http://www.esf.edu/adm/, then disappear when the user mouses over the left column menu (I can't use the script on http://www.esf.edu/adm/ because the mouseovers don't work on Mozilla. A Mozilla fix for this script would also be a solution I could use).

I do not need the original content to reappear at any point. All it need is for it to be there on page load, and disappear with the first menu mouseover.

Thanks
Copy linkTweet thisAlerts:
@phpnoviceJun 08.2005 — As an example, you could do the following to have the first submenu showing by default:
<i>
</i>&lt;script&gt;
[COLOR=Red]var menunotset = true;[/COLOR]
window.onload = function() {
var cc = document.getElementById("submenu").childNodes;
for(var i = 0; i &lt; cc.length; i++) {
if(cc[i].nodeName &amp;&amp; cc[i].nodeName.toLowerCase() == "li") {
[COLOR=Red] if (menunotset) {
cc[i].className = "hover";
menunotset = false;
}[/COLOR]
cc[i].onmouseover = function() {
if(window.cmenu &amp;&amp; window.cmenu != this) window.cmenu.className = "";
(window.cmenu = this).className = "hover"
}
}
}
}
&lt;/script&gt;
Copy linkTweet thisAlerts:
@ottoworksauthorJun 09.2005 — Thanks phpnovice--

It works great, except for one problem. If the user doesn't drag his cursor across the top of the left menu before mousing over other left menu items, the first submenu doesn't disappear and all the other submenus overlap the first. Check it out:

http://www.esf.edu/aec/defaultcss.htm

It's the same on IE and Firefox, Mac and Win, and Safari.

here's the whole script as it stands:

<script>
var menunotset = true;

window.onload = function() {

var cc = document.getElementById("submenu").childNodes;

for(var i = 0; i < cc.length; i++) {

if(cc[i].nodeName && cc[i].nodeName.toLowerCase() == "li") {

if (menunotset) {

cc[i].className = "hover";

menunotset = false;

}

cc[i].onmouseover = function() {

if(window.cmenu && window.cmenu != this)

window.cmenu.className = "";

(window.cmenu = this).className = "hover"

}

}

}

}

</script>



Here's the code for the first to menu items:



<ul id="submenu" style="position:relative">
<li style="padding: 1px">
<ul>
<li>Greetings and welcome to the AEC
</ul>
</li>
<li><a href="/aec/about"><b>About AEC</b></a>
<ul>
<li><a href="/aec/about/default.htm">Overview</a>
<li><a href="/aec/facilities/hwf.htm">Huntington Wildlife Forest</a>
<li><a href="/aec/staff/default.htm">Personnel</a>
</ul>
</li>


I experimented with the first <li>, trying adding text, leaving it empty, having padding=0, etc. Nothing helped. I stuck with padding=1px to give it some dimension, because if I go with 0 and it has no dimension, then this overlap problem happens no matter where the cursor enters the menu.

Can you (or anyone) take me the last step? Thanks.

Cheers,

Paul
Copy linkTweet thisAlerts:
@phpnoviceJun 09.2005 — That is because you created a fake menu entry that is not formatted in such a way as to allow the JavaScript and CSS code to handle it in the same manner as the other menus.
Copy linkTweet thisAlerts:
@phpnoviceJun 09.2005 — Actually, I take that back... Now that I look at it again, I see that the code is saving the currently showing menu. So, change the code I gave you to the following instead:
<i>
</i>if (menunotset) {
(window.cmenu = cc[i]).className = "hover";
menunotset = false;
}
Copy linkTweet thisAlerts:
@ottoworksauthorJun 09.2005 — Eureka! Perfect!

Thank you for your help phpnovice. You made my day.

cheers,

Paul Otteson
Copy linkTweet thisAlerts:
@phpnoviceJun 09.2005 — Cheers.
×

Success!

Help @ottoworks 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 6.18,
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: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

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