DelphiFAQ Home Search:

Creating a dropdown menu with Javascript

 

comments1 comments. Current rating: 3 stars (1 votes). Leave comments and/ or rate it.

Question:

How can I create a navigation menu on the fly?

Answer:

Take the HTML from below. It uses a little graphic for the downpointing arrow. You can download that graphic here. Save it in the same folder where your HTML page is.

The menu will look like this:

 

<html>
<head>
<style>
#ddmenu a{ text-decoration:none; }
#ddmenu a:hover{ background-color:#FFFF95; 
}
</style>
</head>
<body>

<script language="JavaScript1.2">

document.write('<span style="position:relative;width:50px;');
document.write('height:20px;border:1px solid black;font:bold 10pt Verdana;');
document.write('padding:2px" onClick="toggle_menu(1);');
document.write('event.cancelBubble=1" ><span style="cursor:hand;width:100%">');
document.write('Portals <img width=10 height=5 src="down.gif"></span>')
document.write('<div id="ddmenu" style="position:absolute;left:2;top:16;width:90px;');
document.write('height:80px;border:1px solid black;background-color:white;');
document.write('overflow-y:scroll;visibility:hidden;">')

function additem(linkname,dest){ 
  document.write('<b><a target=_blank href="'+dest+'">'+linkname+'</a></b><br>')
}

function toggle_menu(state){
var theMenu=document.getElementById("ddmenu").style;
if (state==0) { 
  theMenu.visibility="hidden" }
else {
  theMenu.visibility = (theMenu.visibility=="hidden") ? "visible" : "hidden";
}
}

additem("Google","http://www.google.com");
additem("Yahoo","http://www.yahoo.com");
additem("Teoma","http://www.teoma.com");

document.onclick= function() {toggle_menu(0); }
document.write('</div></span>')
</script>


</body>
</html>

Comments:

2006-05-17, 04:18:43
anonymous from India  
rating

 

 

Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

Show city and country
Show country only
Hide my location
Leave your comment here:
Please type in the code:
photo Add a picture:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity. The owner of this web site reserves the right to delete such material.