function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
KenBarlowKenBarlow 

How to access javascript variable in static resources for lightning component js helper?

I've got a lightning component that I'm working on and I can see that that javascript file is loading properly (in chrome developer console) from static resources as shown below. 
 
<ltng:require scripts="{!join(',' ,
			$Resource.Main_Assets + '/scripts/embed.min.js',
		)}"
		afterScriptsLoaded="{!c.doInit}"	
	/>

The problem is that I'm attempting to access a variable within that javascript file through a lightning component helper.js file.

Lightning Component - Home.cmp
- Loads the static resource js file.

Lightning Component JavaScript Controller File - HomeController.js
- Calls a function on the helper to then access and use the variable.
doInit: function(cmp, event, helper){            helper.setupItem();
}




Lightning Component JavaScript Helper File - HomeHelper.js
setupItem: function() {
// This is where I'm trying to access the value from the static resource, but it's not letting me access it.

console.log(variableNameFromStaticResourceJSFile);

);

The question is, how can I access that variable from the static resource all the way down inside of the HomeHelper.js file?

Thanks.


 
Ajay K DubediAjay K Dubedi
Hi knbrlo,

I guess you are wrongly calling variable do something like this:

{!URLFOR($Resource.resourceName, 'img/imageName.jpg')} 

This is example of setting url with the value of resourceName with path as img/
<apex:image url="{!URLFOR($Resource.resourceName, 'img/imageName.jpg')}" width="50" height="50"/>


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks,
Ajay Dubedi
Deepali KulshresthaDeepali Kulshrestha
Hi,
Greetings to you!

It should be like this : -
 
Controller JS : -

    doInit: function(cmp, event, helper){                    
        helper.setupItem(cmp, event, helper);
    }
    
Helper JS : -

    setupItem: function(cmp, event, helper) {
        
        console.log(variableNameFromStaticResourceJSFile);
    );


I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Deepali Kulshrestha.