When building Lightning Components, using the HTML script markup is not supported. However, there’s an alternative that is easy to implement. Follow these step-by-step instructions to learn how to use external JavaScript or jQuery libraries within Salesforce Lightning Components.

Upload a Static Resource

Before you can use the jQuery library within your Lightning Component, you need to create a static resource for the JavaScript files.

Static resources can be uploaded as a single file or a group of files in a ZIP folder. To upload a static resource go to Setup > Custom Code > Static Resources.

Loading a Single JS File

When building Lightning Components, using the HTML script markup is not supported and will result in this error message: “script tags only allowed in templates”. The alternative way is to use ltng:require which enables you to load external JavaScript libraries.


    

Static resources can be included using the standard syntax: {!$Resource.yourResourceName + ‘/yourFileName.js’}.


Loading Multiple JS Files

If you have more than one JavaScript file the syntax is a bit different due to a quirk in the way $Resource is parsed in expressions.


 


Use the jQuery library within a component

Once the jQuery libraries are loaded on the page, to utilize the library include the afterScriptsLoaded event and place the method within your client-side controller.

Component markup


    


Client-side controller markup

({

  doInit : function(component, event, helper) {
         
      jQuery("document").ready(function(){
          console.log('loaded');
          
      });
        
 }
})

Example

Here’s a example of using jQuery One Page Nav within a Lightning Component:

Component


   


Client-side controller

({

  doInit : function(component, event, helper) {
         
      jQuery("document").ready(function(){
          $('#cmp-nav').onePageNav({
              scrollSpeed: 750,
				scrollThreshold: 0.5,
				filter: '',
				easing: 'swing',   
          });
          
      });
        
 }
})

Questions?

Send them to me via email, send me a tweet @jennamolby, or leave a comment

Author

I'm a Freelance Marketing Operations Consultant With 15 years of experience in Marketing Operations, I’ve worked with a wide range of tools including Salesforce, Marketing Cloud Account Engagement (Pardot), Marketo, and many other sales and marketing platforms. I help teams optimize their tech stacks, improve processes, and get accurate, actionable reporting. Whether it’s setting up your Marketing Automation Platform, building Salesforce reports, managing lead lifecycles, tracking attribution, or integrating your tech stack, I ensure everything is aligned to drive real results.

4 Comments

  1. What a fantastic read on Salesforce. This has helped me understand a lot in Salesforce course. Please keep sharing similar write ups on Salesforce. Guys if you are keen to know more on Salesforce, must check this wonderful Salesforce tutorial and i’m sure you will enjoy learning on Salesforce training.
    https://www.youtube.com/watch?v=5FTe-ah3WBU

  2. I have read your blog “How to Use jQuery Libraries Within Salesforce Lightning Components”

    For this I have try use external js in salesforce this but I am getting Error.

    Uncaught TypeError: Cannot read property ‘expando’ of undefined
    throws

    http://prntscr.com/jriz7r

    Please Help me?

Write A Comment