Activity › Forums › Salesforce® Discussions › How to include external javascript file in Salesforce Lightning Component?
-
How to include external javascript file in Salesforce Lightning Component?
Posted by Abhinav on April 30, 2016 at 2:20 PMHow to include external javascript file in Salesforce Lightning Component?
shariq replied 7 years, 7 months ago 6 Members · 5 Replies -
5 Replies
-
this link will be helpful:
Thanks
- [adinserter block='9']
-
Hello Abhinav,
The Lightning component framework’s only allow those external JavaScript libraries which was upload in Salesforce static resources. In Spring’15 release Salesforce created a new component called <ltng:require> to load libraries while being secure.
Here is the sample for using external file in Lightning Component:-<ltng:require
styles=”/resource/path/to/css1[,/resource/path/to/css2]”
scripts=’/resource//path/to/js1.js,[/resource//path/to/js2.js]’
afterScriptsLoaded=”{!c.controllerFunction}”
/>Notes:-
- Files paths must always start from /resource. i.e they must be a static resource to enforce security.
- CDNs are not allowed at this point because they are outside of Salesforce data centers and can’t be controlled. In the future, we may allow admins to whitelist CDNs for their orgs.
- If you upload a JS or CSS directly into Static resource, then you should reference them by resource name without .css or .js suffix. For example: /resource/myjsfile or /resource/mycssfile
- If your resource is inside a zip file, then you should use .css or .js suffix. For example: /resource/<resourcename>/path/to/myjsfile.js
-
Hello,
To reference a JavaScript library that you’ve uploaded as a static resource, use a <ltng:require> tag in your .cmp or .app markup.
<ltng:require scripts=”{!$Resource.***resourceName***}”
afterScriptsLoaded=”{!c.afterScriptsLoaded}” />Thanks.
-
The Lightning component framework’s only allow those external JavaScript libraries which was upload in Salesforce static resources. In Spring’15 release Salesforce created a new component called <ltng:require> to load libraries while being secure.
Here is the sample for using external file in Lightning Component:-<ltng:require
styles=”/resource/path/to/css1[,/resource/path/to/css2]”
scripts=’/resource//path/to/js1.js,[/resource//path/to/js2.js]’
afterScriptsLoaded=”{!c.controllerFunction}”
/> -
Try this –
<ltng:require scripts=”{!$Resource.nameOfResource}”
afterScriptsLoaded=”{!c.afterScriptsLoaded}” />Hope this helps.
Log In to reply.