Re: javascript variables undefined Firefox
- From: RobG <rgqld@xxxxxxxxxxxx>
- Date: Tue, 29 Nov 2005 23:54:21 GMT
jimmygoogle wrote:
I posted earlier with a scope problem. I think I resolved it in IE but in Firefox it still exists. Anyone have any ideas/experience with this? I attached my code sorry it is so long. You can cut/paste it into 2 files and run it to see what I mean.
It has nothing to do with scope, it's a surprise to me that it is 'fixed' in IE.
When posting code, use 2 or 4 spaces for indents, not tabs, and manually wrap it at about 70 characters to prevent auto-wrapping by news readers - it will almost certainly introduce errors and obfuscate any that were there in the first place.
###############menu.html############### <html> <body>
<script type="text/javascript">
var xmlDoc;
if (document.implementation && document.implementation.createDocument){ alert ("MOZ"); xmlDoc = document.implementation.createDocument("", "", null); xmlDoc.onload = createTable; }
else if (window.ActiveXObject){ alert ("IE"); xmlDoc = new ActiveXObject("Microsoft.XMLDOM"); xmlDoc.onreadystatechange = function () { if (xmlDoc.readyState == 4) createTable() }; }
else{ alert('Your browser can\'t handle this script'); //return; }
xmlDoc.load("menu.xml");
function createTable(){ menuItems = xmlDoc.getElementsByTagName("menuitem");
You define menuItems here as a global variable inside a function that is not called until the xml file has finished loading.
var e; var nodeid; var parentid; var labelTxt; var linkTxt;
for (var i=0;i<menuItems.length;i++){ // assign each element of the XML file to a variable e=menuItems[i]; childrenTxt = e.getElementsByTagName("node")[0].firstChild.data; heightTxt = e.getElementsByTagName("parent")[0].firstChild.data; labelTxt = e.getElementsByTagName("label")[0].firstChild.data; linkTxt = e.getElementsByTagName("link")[0].firstChild.data; widthTxt = e.getElementsByTagName("link")[0].firstChild.data;
parentmenu = 'Menu' + childrenTxt;
menuchildren=0; menuwidth=80; menuheight=20;
eval(parentmenu + '= new Array(' + '"' + labelTxt + '"' + ',' + '"' + linkTxt + '"' + ',"",' + menuchildren + ',' + menuheight + ',' + menuwidth +');');
Here you use eval to create an unknown number of arrays as global variables - why not use a single object to hold them all, as you've been shown?
} //end for
} // end createTable
function Go(){return}
/*********************************************************************** YOU CAN MANUALLY CHANGE THE MENU HERE, BUT BE VERY CAREFUL ! ***********************************************************************/
var NoOffFirstLineMenus=menuItems.length; // Number of first level
Here you try to use menuItems, but the function that initialises it may not have been called yet. I can only guess that IE has finished loading the file by the time the interpreter gets to here but Firefox hasn't.
I suspect that when you deploy this IE will suffer a similar fate - or maybe the page stops loading completely while the XML file is loaded. It seems a pointless use of XMLHttpRequest in any case.
I can't understand why you need a script that generates over 100 global variables and requires nearly 700 lines of unmaintainable code just to create a menu that could have been done with a few lines of CSS and HTML and not a single line of script.
The code has so many examples of bad coding it's beyond redemption.
e.g. eval is used 30 or 40 times like this:
WMnu = MName + eval(i);
and
NoOffSubs = eval(WMnu + '[3]');
Logic is obfuscated like this:
if(SubLt+CCW>ScWinWdth+LScrlld)SubLt=this.Level==1?ScWinWdth+LScrlld-CCW:SubLt-(CCW+(1-2*ChildOverlap)*ThisWt);
Many variables seem to be created for the sake of it:
var LowBgColor='0080FF'; ...
function MbrSetUp(MmbrCntnr,PrMmbr,WhatMenu,Wdth,Hght){
...
var a,b,c,d;
...
if(RcrsLvl==1){
a=LowBgColor; ...
this.LoBck=a;
}[...]
/*********************************************************************** DO NOT EDIT ANYTHING BELOW THIS LINE - IT WILL BREAK THE SCRIPT ! ***********************************************************************/
I don't wonder.
[...]
-- Rob .
- Follow-Ups:
- Re: javascript variables undefined Firefox
- From: RobG
- Re: javascript variables undefined Firefox
- References:
- javascript variables undefined Firefox
- From: jimmygoogle
- javascript variables undefined Firefox
- Prev by Date: Re: XmlHttpRequest not loading latest version of xml
- Next by Date: javascript sending variable
- Previous by thread: javascript variables undefined Firefox
- Next by thread: Re: javascript variables undefined Firefox
- Index(es):
Relevant Pages
|