
Originally Posted by
gp_330
The code I have works in Firefox perfectly (of course),
That's because Firefox doesn't need the code.
but of course IE only has one set of drop downs working.
Your assignment to the onload property doesn't work the way you seem to think it should. More on that later.
Code:
startList = function() {
if (document.all&&document.getElementById) {
navRoot = document.getElementById("menu1");
for (i=0; i<navRoot.childNodes.length; i++)
{
node = navRoot.childNodes[i];
if (node.nodeName=="LI")
{
node.onmouseover=function() {this.className+=" over";}
node.onmouseout=function() {this.className=this.className.replace(" over", "");}
}
}
}
}
If this is what the original code looks like, it's very badly written.
You don't need to duplicate the functions, by the way. The only thing that differs between them is the argument to the getElementById method, so add a parameter that specifies that value.
Code:
var initialiseMenu = function() {
var menus = [];
function setClass() {
if (!/(^|\s)over(\s|$)/.test(this.className))
this.className += ' over';
}
function removeClass() {
this.className = this.className.replace(/(^|\s)over(\s|$)/, ' ');
}
window.attachEvent('onunload', function() {
for (var i = 0; i < menus.length; ++i) {
menus[i].detachEvent('onmouseover', setClass);
menus[i].detachEvent('onmouseout', removeClass);
}
menus.length = 0;
window.detachEvent('onunload', arguments.callee);
});
return function(id) {
var menu = document.getElementById(id),
nodes = menu.childNodes;
for (var index = 0, length = nodes.length, node; index < length; ++index) {
node = nodes[index];
if (node.tagName == 'LI') {
node.attachEvent('onmouseover', setClass);
node.attachEvent('onmouseout', removeClass);
}
}
menus[menus.length] = menu;
};
}();
window.attachEvent('onload', function() {
initialiseMenu('menu1');
initialiseMenu('menu2');
window.detachEvent('onload', arguments.callee);
});
You should be aware of two things. The first is that this isn't tested: you didn't provide a link to your code. The second is that the above isn't exactly an example of good practice. It makes a heap of assumptions, but that's because you should place it in an external file and include it with:
HTML Code:
<!--[if IE]><script type="text/javascript" src="/path/to/file"></script><![endif]-->
setting the src attribute appropriately. Only IE versions 5+ will see this code, so the assumptions that would break the code in other browsers won't matter here.
In case you're wondering why the code is larger, much of it is structured to allow the event listeners to be removed to try and avoid IE's notorious memory leak (which the original code will invoke). If you used this menu with the original code in several documents, the leak would grow as the user moved from document to document, and that clearly isn't a good thing.
window.onload=startList;startList2
The problem here is that there are two distinct statements. It could have just as easily been written as:
Code:
window.onload = startList;
startList2;
You can only assign one function to the onload property, so that function must invoke the other two:
Code:
window.onload = function() {
startList();
startList2();
};
If you have problems with what I've proposed, post a link to an example (especially if it fails) otherwise I won't help you (I'm not a mind reader!).
Good luck,
Mike
Bookmarks