Page 1 of 1

Updating Global Variables

Posted: Sun Mar 23, 2008 2:10 pm
by HiddenS3crets
I have two javascript functions defined below:

Code: Select all

var str
function display()
{
  setStr()
  document.write(str)
}
 
function setStr()
{
  str = "hello world"
}
Instead of display() displaying "hello world", it says that the variable is undefined

Why is it that str is not updated before document.write() is called?

Re: Updating Global Variables

Posted: Sun Mar 23, 2008 8:06 pm
by JellyFish
Where are you calling display?

Re: Updating Global Variables

Posted: Sun Mar 23, 2008 8:33 pm
by HiddenS3crets
In a html file

Re: Updating Global Variables

Posted: Mon Mar 24, 2008 12:50 am
by JellyFish
before or after you define the functions and the variable?

Re: Updating Global Variables

Posted: Mon Mar 24, 2008 10:00 am
by HiddenS3crets
I've tested this again and it seems to be working, but in a more complicated function involving the updating of a global variable it does not get updated before it is needed.