Re: Netscape says variable not defined?

by "Rod Hutson" <rbhutson(at)ieee.org>

 Date:  Tue, 13 Feb 2001 21:11:54 -0700
 To:  "HWG Style" <hwg-style(at)hwg.org>,
"HWG Techniques" <hwg-techniques(at)hwg.org>
 References:  atfantasy atfantasy2
  todo: View Thread, Original
Ed,

I could be wrong, but it looks like two things are contributing to your
situation:

1. the line

"anch = document.anchors[i];"

doesn't really define anch, but just assigns a value to it from the array...
you should probably explicitly declare "anch" at the very top of your
function before its first use.

2. the paragraph

>"for (i=0;i<document.anchors.length;i++) {
>                          anch = document.anchors[i];
>                          if(anch.name == n) break;
>                  }"

may be suffering from "scope issues" where "anch" only exists while you are
in the for-loop.  Once you exit the loop, the variable is out of scope, and
doesn't exist anymore.

Or, of course, I could be too tired and simply rambling on and on and on...

Add the explicit declaration of the variable at the top of your function and
you should be okay.  I have no idea why MS IE accepts what is generally
considered bad programming style (nothing personal), while Netscape chokes
on it.

Rod Hutson
email: rbhutson(at)ieee.org


----- Original Message -----
From: "Ed Lazor" <osmosis(at)atfantasy.com>
To: "HWG Style" <hwg-style(at)hwg.org>; "HWG Techniques"
<hwg-techniques(at)hwg.org>
Sent: 13 February, 2001 8:39 PM
Subject: Netscape says variable not defined?


> Netscape's reporting an error that doesn't make sense:  "line 26:  anch is
not
> defined".  The page in question has a javascript that defines the value of
> the variable
> in a javascript.  It doesn't make any sense, because the variable is
> defined a few lines
> before the error is generated.  Here's the function generating the error:
>
> function getAnch(n) {
>          if (NS4) {
>                  for (i=0;i<document.anchors.length;i++) {
>                          anch = document.anchors[i];
>                          if(anch.name == n) break;
>                  }
>                  menX = anch.x;
>                  menY = anch.y;
>          }
>          if (IE4) {
>                  menX = tbAnch.offsetLeft;
>                  menY = tbAnch.offsetTop;
>          }
>          return [menX,menY];
> }
>
> The web page itself is located at:
>
>      http://portal.atfantasy.com/includes/hm4_4/LoadMe.html
>
> Internet Explorer renders the page accurately and I'm not sure why
Netscape
> generates the error.  Could you take a look at it and let me know what the
> problem
> is?
>
> Thanks! =)
>
> -Ed
> osmosis(at)atfantasy.com
>

HWG hwg-style mailing list archives, maintained by Webmasters @ IWA