Re: javascript variables undefined Firefox



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 .



Relevant Pages

  • Re: Script doesnt work in Firefox
    ... The following code works fine in IE but not Firefox. ... It's a little script that zooms an image and resizes the window to fit. ... I guess strictly you should also test for getElementById and offer document.all if it is not available, but that is only really necessary if you want to support IE 4. ... var int_max_w = window.screen.availWidth; ...
    (comp.lang.javascript)
  • Re: question about javascript
    ... > my data xml file is a .aspx page). ... > var data = new ActiveXObject; ... >>> Somewhere in my xsl file I have a script that is repeated many times: ...
    (microsoft.public.scripting.jscript)
  • Re: question about javascript
    ... > my data xml file is a .aspx page). ... > var data = new ActiveXObject; ... >>> Somewhere in my xsl file I have a script that is repeated many times: ...
    (microsoft.public.dotnet.framework.aspnet)
  • Error Object Expected
    ... The script works perfect on Firefox, ... but Internet Explorer keeps complaining about "Error Object Expected" ... var p = new Array ...
    (comp.lang.javascript)
  • Newbie needs help with setInterval
    ... script from Dynamic Drive's Web site and I'm trying to make a simple ... What I would like to do is to have the Ajax ticker periodically go out ... // getXMLfile- Use Ajax to fetch xml file ... var instanceOfTicker=this ...
    (comp.lang.javascript)