Topic: My global variable - why is it undefined? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=31277" title="Pages that link to Topic: My global variable - why is it undefined? (Page 1 of 1)" rel="nofollow" >Topic: My global variable - why is it undefined? <span class="small">(Page 1 of 1)</span>\

 
ruffy
Nervous Wreck (II) Inmate

From:
Insane since: Jun 2008

posted posted 08-28-2009 18:11

I code my global variables first,
then my window.onload function, like this:

code:
var globvar = {
  oneTimeSwitch: "0",	
  kbinput: document.createElement("INPUT"),
  kbdivision: document.createElement("DIV"),
etc.
}

window.onload = function() {
  if (kbinput==undefined) alert("NO KBINPUT");
  setTriggers();
}


Why do I get the error:
"KBINPUT IS NOT DEFINED"?
and
Why doesn't the IF statement for UNDEFINED element
catch the problem?

poi
Paranoid (IV) Inmate

From: Norway
Insane since: Jun 2002

posted posted 08-29-2009 10:22

kbinput is only defined in globvar. kbinput itself does not exit. It is undefined. But globvar.kbinput exists.

DavidJCobb
Nervous Wreck (II) Inmate

From: United States
Insane since: Mar 2009

posted posted 08-29-2009 22:50

As poi said, kbinput is undefined because you never defined it. The reason you received the error rather than seeing "NO KBINPUT", however, is because you can't check for undefined global variables using the == operator unless you check them in terms of the window object. E.x. kbinput==undefined throws an error, but window.kbinput==undefined would show your error message. Object properties need not be prepended with "window.", however -- globvar.kbinput==undefined would work just fine.

----------------------

coach
Nervous Wreck (II) Inmate

From:
Insane since: May 2011

posted posted 05-31-2011 11:07
Edit TP: spam removed


Post Reply
 
Your User Name:
Your Password:
Login Options:
 
Your Text:
Loading...
Options:


« BackwardsOnwards »

Show Forum Drop Down Menu