wrote:
> hi,
> so i'm creating a dynamic drop-down menu. the menu and the text show up
> fine in IE but only the drop-down shows in Firefox without the menu
> text. Below is the fxn code. help pls.
>
> function DropDownHelper(menuArray, top, left, height)
> {
> var currItem = new String();
Please don't use tabs in posted code. Use 2 or 4 spaces for indents and
wrap lines manually at about 70 characters.
There is no need for a string object, you can initialise it as a string
using:
var currItem = '';
> var item;
> var idStr;
>
>
> for(var i = 0; i < menuArray.length; i++)
> {
> currItem = menuArray[i];
> currItemArr = currItem.split(";");
This will create currItemArr as a global variable, is that neccessary?
var currItemArr = currItem.split(";");
>
> item = document.createElement("div");
You should test for support before using DOM features:
if (document.createElement) {
document.createElement('div');
// ...
> idStr = "dropDownMenuId" + i;
>
> ExistingMenuItemIds[i] = idStr;
Where was ExistingMenuItemIds declared? Is it a global variable?
>
> item.setAttribute('id', idStr);
Simpler to use:
item.id = idStr;
> item.style.visibility = "visible";
You should test for support for style objects, using a class is much
simpler. Div's are visibile by defalut, there is not need to set them
as visible unless you have previously set it to hidden.
if (item.style){
item.style.visibility = 'visible';
> item.style.top = top;
Where was 'top' defined? What is its value?
> item.style.fontFamily = "Arial";
> item.style.fontSize = "13pt";
Font sizes should be set using proportional units - em, en, etc.
> item.style.color = "white";
> item.style.fontWeight = "bold";
> item.style.position = "absolute";
> item.style.height = height;
> item.style.left = left;
Where are 'height' and 'left' defined?
> item.style.width = "200px";
> item.style.backgroundColor = "red";
> item.style.color = "white";
That's the second time you've set the color attribute.
> item.style.cursor = "hand";
'hand' is not a valid CSS value for the the cursor, use 'pointer'
(though I think some older versions of IE may require 'hand').
>
> item.innerHTML = "<p onclick = \"GoTo('" + currItemArr[1] + "')\">" +
> currItemArr[0] + "</p>";
You've used DOM for everything else, why not here too? It's a much
safer way of adding the onclick:
var oP = document.createElement('p');
oP.appendChild(document.createTextNode(currItemArr[0]));
oP.onclick = function(){GoTo(currItemArr[1])};
>
>
> document.body.appendChild(item);
> var newtop = GetIntFromProperty(top) + height;
What does 'GetIntFromProperty()' do? Presumably it trims 'px' from the
value of 'top'. Why not set top as an integer and concatenate 'px' when
you set the style property?
Where you declare your other variables, add:
var newtop = 0;
Presumably you want to set the div's 'top' attribute as:
item.style.top = newtop + 'px';
And here:
newtop += top;
> top = newtop + "px";
> }
> }
>
I can't test the code because you haven't given sufficient information.
There are thousands of drop-down menus out there, many based on CSS
that use very little JavaScript, some use none. Quite a few are offered
completely unencumbered.
Why not just use one of those? There are quite a few offered here:
<URL:http://www.alistapart.com/topics/code/css/>
--
Rob