473,581 Members | 2,833 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Cascading menu script

Below is a script I found at http://javascript.internet.com/ for a
cascading menu. The script works great but there is one thing that I
would like modified. BecauseI am just learning javascript, I did not
want to try to modify the code without a little help. When you place
the mouse over the menu bar, this script calls the function to show the
menu. I would like it modified to hide the menu when the mouse is
removed. Please help. The full code is below:

function InitMenu()
{
var bar = menuBar.childre n

for(var i=0;i < bar.length;i++)
{
var menu=eval(bar[i].menu)
menu.style.visi bility = "hidden"
bar[i].onmouseover = new Function("ShowM enu("+bar[i].id+")")
var Items = menu.children
for(var j=0; j<Items.length ; j++)
{
var menuItem = eval(Items[j].id)

if(menuItem.men u != null)
{
menuItem.innerH TML += "<Span
Id="+menuItem.i d+"_Arrow class='Arrow'>4 </Span>"
//var tmp = eval(menuItem.i d+"_Arrow")
// tmp.style.pixel Left =
menu.getBoundin gClientRect().R ight //- tmp.offsetWidth - 15
FindSubMenu(men uItem.menu)}

if(menuItem.cmd != null)
{
menuItem.onclic k = new
Function("Do("+ menuItem.id+")" ) }

menuItem.onmous eover = new
Function("highl ight("+Items[j].id+")")

}

}
}
function FindSubMenu(sub Menu)
{
var menu=eval(subMe nu)
var Items = menu.children
for(var j=0; j<Items.length ; j++)
{
menu.style.visi bility = "hidden"
var menuItem = eval(Items[j].id)
if(menuItem.men u!= null)
{
menuItem.innerH TML += "<Span
Id="+menuItem.i d+"_Arrow class='Arrow'>4 </Span>"
// var tmp = eval(menuItem.i d+"_Arrow")
//tmp.style.pixel Left = 35
//menuItem.getBou ndingClientRect ().right - tmp.offsetWidth - 15
FindSubMenu(men uItem.menu)
}

if(menuItem.cmd != null)
{
menuItem.onclic k = new
Function("Do("+ menuItem.id+")" ) }

menuItem.onmous eover = new
Function("highl ight("+Items[j].id+")")

}
}
function ShowMenu(obj)
{
HideMenu(menuBa r)
var menu = eval(obj.menu)
var bar = eval(obj.id)
bar.className=" barOver"
menu.style.visi bility = "visible"
menu.style.pixe lTop = obj.getBounding ClientRect().to p +
obj.offsetHeigh t + Bdy.scrollTop
menu.style.pixe lLeft = obj.getBounding ClientRect().le ft +
Bdy.scrollLeft
}

function highlight(obj)
{
var PElement = eval(obj.parent Element.id)
if(PElement.has ChildNodes() == true)
{ var Elements = PElement.childr en
for(var i=0;i<Elements. length;i++)
{
TE = eval(Elements[i].id)
TE.className = "menuItem"
}
}
obj.className=" ItemMouseOver"
window.defaultS tatus = obj.title
ShowSubMenu(obj )
}

function Do(obj)
{
var cmd = eval(obj).cmd
window.navigate (cmd)

}

function HideMenu(obj)
{
if(obj.hasChild Nodes()==true)
{
var child = obj.children

for(var j =0;j<child.leng th;j++)
{
if (child[j].className=="ba rOver")
{var bar = eval(child[j].id)
bar.className=" Bar"}

if(child[j].menu != null)
{
var childMenu = eval(child[j].menu)
if(childMenu.ha sChildNodes()== true)
HideMenu(childM enu)

childMenu.style .visibility = "hidden"
}
}

}
}
function ShowSubMenu(obj )
{
PMenu = eval(obj.parent Element.id)
HideMenu(PMenu)
if(obj.menu != null)
{
var menu = eval(obj.menu)
menu.style.visi bility = "visible"
menu.style.pixe lTop = obj.getBounding ClientRect().to p +
Bdy.scrollTop
menu.style.pixe lLeft = obj.getBounding ClientRect().ri ght +
Bdy.scrollLeft
if(menu.getBoun dingClientRect( ).right window.screen.a vailWidth )
menu.style.pixe lLeft = obj.getBounding ClientRect().le ft -
menu.offsetWidt h
}
}

Jan 20 '07 #1
1 2155

Anthony wrote:
Below is a script I found at http://javascript.internet.com/ for a
cascading menu. The script works great but there is one thing that I
would like modified. BecauseI am just learning javascript, I did not
want to try to modify the code without a little help. When you place
the mouse over the menu bar, this script calls the function to show the
menu. I would like it modified to hide the menu when the mouse is
removed. Please help. The full code is below:

function InitMenu()
{
var bar = menuBar.childre n

for(var i=0;i < bar.length;i++)
{
var menu=eval(bar[i].menu)
menu.style.visi bility = "hidden"
bar[i].onmouseover = new Function("ShowM enu("+bar[i].id+")")
var Items = menu.children
for(var j=0; j<Items.length ; j++)
{
var menuItem = eval(Items[j].id)

if(menuItem.men u != null)
{
menuItem.innerH TML += "<Span
Id="+menuItem.i d+"_Arrow class='Arrow'>4 </Span>"
//var tmp = eval(menuItem.i d+"_Arrow")
// tmp.style.pixel Left =
menu.getBoundin gClientRect().R ight //- tmp.offsetWidth - 15
FindSubMenu(men uItem.menu)}

if(menuItem.cmd != null)
{
menuItem.onclic k = new
Function("Do("+ menuItem.id+")" ) }

menuItem.onmous eover = new
Function("highl ight("+Items[j].id+")")

}

}
}
function FindSubMenu(sub Menu)
{
var menu=eval(subMe nu)
var Items = menu.children
for(var j=0; j<Items.length ; j++)
{
menu.style.visi bility = "hidden"
var menuItem = eval(Items[j].id)
if(menuItem.men u!= null)
{
menuItem.innerH TML += "<Span
Id="+menuItem.i d+"_Arrow class='Arrow'>4 </Span>"
// var tmp = eval(menuItem.i d+"_Arrow")
//tmp.style.pixel Left = 35
//menuItem.getBou ndingClientRect ().right - tmp.offsetWidth - 15
FindSubMenu(men uItem.menu)
}

if(menuItem.cmd != null)
{
menuItem.onclic k = new
Function("Do("+ menuItem.id+")" ) }

menuItem.onmous eover = new
Function("highl ight("+Items[j].id+")")

}
}
function ShowMenu(obj)
{
HideMenu(menuBa r)
var menu = eval(obj.menu)
var bar = eval(obj.id)
bar.className=" barOver"
menu.style.visi bility = "visible"
menu.style.pixe lTop = obj.getBounding ClientRect().to p +
obj.offsetHeigh t + Bdy.scrollTop
menu.style.pixe lLeft = obj.getBounding ClientRect().le ft +
Bdy.scrollLeft
}

function highlight(obj)
{
var PElement = eval(obj.parent Element.id)
if(PElement.has ChildNodes() == true)
{ var Elements = PElement.childr en
for(var i=0;i<Elements. length;i++)
{
TE = eval(Elements[i].id)
TE.className = "menuItem"
}
}
obj.className=" ItemMouseOver"
window.defaultS tatus = obj.title
ShowSubMenu(obj )
}

function Do(obj)
{
var cmd = eval(obj).cmd
window.navigate (cmd)

}

function HideMenu(obj)
{
if(obj.hasChild Nodes()==true)
{
var child = obj.children

for(var j =0;j<child.leng th;j++)
{
if (child[j].className=="ba rOver")
{var bar = eval(child[j].id)
bar.className=" Bar"}

if(child[j].menu != null)
{
var childMenu = eval(child[j].menu)
if(childMenu.ha sChildNodes()== true)
HideMenu(childM enu)

childMenu.style .visibility = "hidden"
}
}

}
}
function ShowSubMenu(obj )
{
PMenu = eval(obj.parent Element.id)
HideMenu(PMenu)
if(obj.menu != null)
{
var menu = eval(obj.menu)
menu.style.visi bility = "visible"
menu.style.pixe lTop = obj.getBounding ClientRect().to p +
Bdy.scrollTop
menu.style.pixe lLeft = obj.getBounding ClientRect().ri ght +
Bdy.scrollLeft
if(menu.getBoun dingClientRect( ).right window.screen.a vailWidth )
menu.style.pixe lLeft = obj.getBounding ClientRect().le ft -
menu.offsetWidt h
}
}
General notes on first glance:

1. This script will only work on Internet Explorer, because it uses the
IE-specific DOM. Use element attributes node.childNodes and
node.parentNode instead of IE-specific node.children and
node.parentElem ent.

2. I'm not sure why this script uses "eval" so much. It would appear
that it's attempting to use "eval" to parse attributes to strings? It
doesn't really make any sense. In general (read this group's FAQ), you
want to avoid using eval wherever possible, and I'm not convinced that
this script needs to use eval at all. -- Ok, after looking at this
script more closely, I see why it's using eval. It's another IE-only
thing: IE has a JS reference to each node with an ID attribute with the
variable name of that ID. So for instance, <div id="menu"will be a
JS variable called "menu". No other browser supports this, that I'm
aware of, so you should replace "eval" with document.getEle mentById.

3. Lots and lots of mediocre scripts and textbooks out there show code
like "node.style.vis ibility = 'hidden'" and
"node.className ='something'". This works ok with smaller scripts but
you'll quickly run into trouble on complex websites where nodes have
many classnames. It's best to have helper functions that will add or
remove classnames without setting the "className" property, thereby
overwriting whatever other classes the element might have. Also, just
a personal preference, but I like to avoid assigning any "style"
properties on elements except for positioning. Use classes for
everything else, like color, visibility, border, etc.

4. Forget that website. Remove it from your bookmarks, forget it even
exists. Most of the scripts there are ancient and have horrible coding
practices, like only writing for one browser. When you're learning
don't just read "random scripts you found somewhere on the Internet,"
you need good reliable resources you can trust. Pick up a copy of
javascript: The Definitive Guide published by O'Reilly Press (the one
with the rhino on the cover). Read through that, work through some
examples, then let us know if you're still having problems with your
code.

Best,

-DG

Jan 21 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1783
by: Humpty Dumpty | last post by:
Hi folks, here's a challenge: I have a dynamically created cascading menu in Tkinter that can be quite large because it is created from a file. I tried using lazy creation so only the menu item that is actually selected by the user gets children menu items created under it, but that fails. I did this by use of postcommand callback, in which...
3
3575
by: michelle | last post by:
Hi all, I am new to Tk, or Python GUI programming and I seem to be stuck. I have looked about for help with Tk GUIs, but everything seems so terse or incomplete?? I have been mostly using the "Introduction to Tkinter" by Fredrik Lundh (http://www.pythonware.com/library/tkinter/introduction/index.htm) What I am trying to do is add...
2
1509
by: gregory stevenson | last post by:
I am looking for a simple cascading menu javascript.One where it lives on the left side of the screen, listed vertically and when you select a menu item the list expands to show the sub menu items within that catagory. This is the only decent one I've been able to find....
7
2712
by: Marci | last post by:
I found this script for cascading menus, however, I cannot reach the author to solve the bug I am having when I add a second menu to it. My problem is this: If I click on the first link, the menu displays well. If I then click on the second link, the first menu from the first link still displays. How do I get make the first menu...
1
1204
by: Cindy Lee | last post by:
is it possible to have cascading menus like: http://www.cracky.net/JAVA/Navigation/cascading-menu.html With scrollbars added, so you can only see a few options at a time? On some of my options I have to many to display, without it looking bad. The only thing I've seen to do this is applets.
1
2007
by: JMosey | last post by:
Not sure if this has been covered ( a google search came up pretty bare). I have a site that: - has multi-level cascading menus - floats center of the browser window - Will have fairly heavy Safari and Firefox views (~25%) Finding a cascading menu is easy, I trip over about half a dozen of those a week. The problem is when you maximize...
1
1643
by: dkirkdrei | last post by:
I am using the javascript below in conjuction with PHP to dynamically build a cascading menu of part assemblies from a database. The cascading menu allows the user to "drill" down to the single part level of larger part assemblies This works very well except for when a person needs to search for a specific item. Edit>Find on this page(ctrl+f)...
0
7876
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8156
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8310
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
8180
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6563
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5681
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5366
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3809
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2307
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.