2

It is possible when we are defining a variable in JS file which will be loaded from static resource and want to use that variable in the Lightning controller as shown in this answer.

I want to do exactly vice versa from the above situation. See explanation below -

  • I will define a variable in Lightning controller and I want to access this variable in the JS file which is loaded from the static resource.

Please give your thoughts. Thanks in advance.

  • I am also analyzing the scenario and trying to resolve this issue. If someone has any thought on it please let me know? Thanks – Kapil Kaushik Jul 3 '17 at 11:27
3

Oh Yes! I got the solution which is mentioned below.

Before the loading of the JS file(which will use that variable) from static resource invoke the function of your lightning controller and write below statement in that function -

initializeVariables : function() {

     window.<VARIABLE_NAME> = 'ANY_VALUE';
}

Surely you will be able to access that variable in your JS file.

Note - Be sure to get your variable initialized before the loading of JS file that will use that variable. Thanks!

  • Is it possible the opposite one, I want to create global variable in the static resource then, I want to get it afterScript loaded. I try but it gives "there is no variable" error. – efirat Dec 21 '17 at 11:31
  • Yeah! Sure we can do this. Did you try the same way of doing the opposite? Because when I was digging the scenario given in the question, I had also tried yours. It will definitely work. In shorts, define the variable in the static resource with window.<variable_name>. – Kapil Kaushik Dec 21 '17 at 18:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.