Updating Global Variables

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

Updating Global Variables

Post 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?
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Updating Global Variables

Post by JellyFish »

Where are you calling display?
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

Re: Updating Global Variables

Post by HiddenS3crets »

In a html file
User avatar
JellyFish
DevNet Resident
Posts: 1361
Joined: Tue Feb 14, 2006 7:18 pm
Location: San Diego, CA

Re: Updating Global Variables

Post by JellyFish »

before or after you define the functions and the variable?
HiddenS3crets
Forum Contributor
Posts: 119
Joined: Fri Apr 22, 2005 12:23 pm
Location: USA

Re: Updating Global Variables

Post 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.
Post Reply