I have a static resource named as 'geoCode', which is a javascript file, containing a function. I want to call this function from lightning component controller and receive an alert message on screen.

function callMe() {
    alert('You are inside static resource');

2 Answers 2


You should be able to do this right inside of your controller code. However, you need to make sure that the call to this function doesn't happen until the script is loaded. This is done by specifying a controller action to be called in the afterScriptsLoaded parameter of <ltng:require>.

In your component:

<ltng:require scripts="/resource/geoCode" afterScriptsLoaded="{!c.scriptsLoaded}"/>

In your controller:

  scriptsLoaded : function(component, event, helper) {

See this for the official docs: Using External JavaScript Libraries with Lightning Components

  • Its giving error as below: Uncaught error in markup://ltng:afterScriptsLoaded : callMe is not defined I have exactly what you said. Actually I even copy pasted your code. then also same error. Sep 15, 2015 at 10:10

I think you need to add one more thing to your component definition:

<ltng:require scripts="/resource/geoCode/YOURFILENAME" afterScriptsLoaded="{!c.scriptsLoaded}"/>

This is because you have named the resource (this is the name you called it on upload) but you have not specifically referenced the actual file (remember to leave off the extension if it's a single file)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .