Skip to main content
April 29, 2009
Question

Changing the value of global javascript variables

  • April 29, 2009
  • 2 replies
  • 3707 views

I'm having trouble finding out how to change the value of a javascript global variable from within another javascript function. Here's what I've been doing so far.

1/ I set the value in the header as follows:

<script type="text/javascript">
    var style = 0;
</script>

2/ I have a function in the header which I want to use to change the value of the global variable

  <script type="text/javascript"> 
function updateStyle(x){
  if (x==1) {  style = 1; }
  if (x==2) {  style = 2; }
if (x==3) {  style = 3; }
if (x==4) {  style = 4; }
alert("style is: " + style);

}
</script>

3/ Then I call the function using onclick.

onclick="updateStyle(1)"

I can see from the 'alert' in the function that the function is being accessed and the value of 'style' within the function is being altered, but it doesn't seem to be the global variable that is being altered. Presumably it is a local version.

The aim is to be able to use the gobal variable's value in the body of the page, but when I examine it, it is always the value set when the global was initialised (ie it is always 0).

At this stage, the code in the body is simply an alert to see what the value is:

   <script type="text/javascript">
        alert("style is: " + style);

  </script>
    

Any ideas?

Richard

This topic has been closed for replies.

2 replies

April 30, 2009

I see. How would I get the body code to trigger?

As part of the onclick I put a call to window.location.reload(); thinking that this would cause the body code to run, but obviously this isn't working.

David_Powers
Inspiring
April 30, 2009

Without knowing what it is you're trying to do, it's difficult to offer any advice. I also hasten to add that my knowledge of JavaScript is limited. I can get things done, but lay no claims to being an expert. Although your question isn't related to Spry, you might have better luck in the Spry forum (http://forums.adobe.com/community/adobe_labs/spry_framework_ajax_prelease). That's where the JavaScript hardballs hang out.

April 30, 2009

I can see what is happening now. I call the function via onclick to update the global varable and this is working fine, but when the page is reloaded, the variable is reset. In effect I am trying to set a varable which can retain its value when the page is reloaded.

If you've any ideas that would be helpful. If not, thanks for your help to date and I'll try the Spry forum as suggested.

David_Powers
Inspiring
April 29, 2009

At this stage, the code in the body is simply an alert to see what the value is:

   <script type="text/javascript">
        alert("style is: " + style);

  </script>

The value of style won't change until you have clicked an element that triggers updateStyle().

April 30, 2009

David,

Doesn't the onclick event do this, i,e onclick="updateStyle(1)".

This is what starts the process off. I use onclick to call updateStyle which (is supposed to) updates the global variable, which I then use later in the body of the page.

If I put an alert into updateStyle to show when it is called, then I can see that this alert is called.

David_Powers
Inspiring
April 30, 2009

Yes, but the alert that you put into the body of the page is triggered before you hit onclick.