23
Views
5
Comments
[High Code Invaders] How to call a js global variable after the screen starts?
high-code-invaders
Reactive icon
Forge component by Lucas Soares

Hi, I would like to understand how global variables work within a script and also how I can change their value after the initial screen loads.


Sorry, maybe I'm in the thread, so if the admin wants to change the forum thread, that's fine.

Scenario:

I have a script that is loaded along with the page, within this script I created a global variable that will be used later in code that already exists.


I think this is ok, but when the main screen starts, I pass a value within a script in OnInitialize "variableGlobal = "caminho_da_imagem";


But for some reason, the image doesn't load.

 
MVP

Hi, 


Have you put the javscript on the scripts properties of the screen ?

Hi Toto, yes.


I placed the javascript code on the page in "required scripts", and in OnInitialize I placed a Javascript informing the value of the global variable, for example "global variable = "image_path";"


However, when the page loads, the image does not appear.

In this case, what is loaded first?

The script within "required scripts" or the "OnInitialize" script?


Thanks.

 
MVP

Hi,


I've create simple test, and the result is OnInitialize will run first, then the Required Scripts.

So maybe you need to change OnInitialize, into OnReady.


Thanks

Hi Hudson,

I think defining variable inside JS of Initialize is not "really" global, it seems scoped inside the JS block. To solve this, inside JS of Initialize, you can declare variable like this: 

document.img_name = 'pikachu.png';

We "binding" that variable to document object. Then in the require Script of the screen, you can access the variable via document like this, for example:

console.log('Image Name: ' + document .img_name );
someFunction(document .img_name);

Hope this helped.

I will try to create global variables using the module's ready-made application (not the page one).

I did a small test and worked on it, now I'm going to put in the settings I was imagining and I'll publish the results soon.

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.