Skip to main content
Alan_AEDScripts
Inspiring
February 16, 2012
Question

Global variables and toolbar button.

  • February 16, 2012
  • 1 reply
  • 992 views

Hi there, running a script in my editor is working fine, can't see what's wrong. Then when the same script is executed via a button in a toolbar, it seems to lose the

defined global variable 'gFeatherPx'....? Any ideas? Thanks a mil.

Run in my script editor - correctly.

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

alert(gFeatherPx)  // gives 5 from a global.

          if(!gFeatherPx){var gFeatherPx = 12;}

alert(gFeatherPx)  // gives 5 from a global.

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

Run from a script invoking button in AFX... can't find the global var.

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

alert(gFeatherPx)  // gives undefined - can't find global.

  if(!gFeatherPx){var gFeatherPx = 12;}

alert(gFeatherPx)  //gives 12.

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

This topic has been closed for replies.

1 reply

Legend
February 17, 2012

How are you declaring the global? Just as a var? My first guess would be that the button is using...

myButton.onClick = function(){  //YOUR CODE };

...to launch the your code? If so then function doesn't seem to recognize a regular variable (var). If you define your global via an Object...

var myObject = new Object();

myObject.gFeatherPx = 20;

...then the function should see it.

Alan_AEDScripts
Inspiring
February 17, 2012

Thanks for the reply...

Instead of :

if(!gFeatherPx){var gFeatherPx = 12;}


I have used:

function checkExistingVariable(){

                                 if (!gFeatherPx){

                                           var gFeatherPx = 12;

                                           }

                              }

in the script that executes when the button is clicked.  Working fine now, so the script will work with and without the global.

Alan_AEDScripts
Inspiring
February 17, 2012

This is some interesting reading I have to come back to - havn't got the time at the minute.

How to check for non defined variables....    

http://stackoverflow.com/questions/858181/how-to-check-a-not-defined-variable-in-javascript