Topic: My global variable - why is it undefined? 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?" rel="nofollow" >Topic: My global variable - why is it undefined?\

 
Author Thread
ruffy
Nervous Wreck (II) Inmate

From:
Insane since: Jun 2008

IP logged posted posted 08-28-2009 18:11 Edit Quote

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

IP logged posted posted 08-29-2009 10:22 Edit Quote

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

IP logged posted posted 08-29-2009 22:50 Edit Quote

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

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


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


« BackwardsOnwards »

Show Forum Drop Down Menu