2

When I'm adding script files to a lightning component i'm getting an error as 'script tags only allowed in templates: Source'. Is there another way to add script links to the lightning application?

  • 1
    This could be helped by some code. What did you attempt? – pchittum Jan 20 '15 at 23:16
4

You cannot include scripts with a script tag from a component. You can only include them with a script tag from the app.

From the Lightning Components Developer’s Guide

To reference a JavaScript library that you’ve uploaded as a static resource, use a <script> tag in your .app resource.

There are alternatives to the script tag mentioned in the Question How to use jQuery (or any JS lib) in initial post rendering of Lightning Components that you could explore.

3

Use ltng:require.

ltng:require enables you to load external CSS and JavaScript libraries after you upload them as static resources.

<ltng:require 

   styles="{!$Resource.SLDSv1 + '/assets/styles/lightning-design-system-ltng.css'}"

   scripts="{!$Resource.jsLibraries + '/jsLibOne.js'}"

   afterScriptsLoaded="{!c.scriptsLoaded}" />

Source:- https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/aura_compref_ltng_require.htm

0

Here we need to create link or script tags dynamically in the component's controller doInit function.

({
doInit : function(component, event, helper) {

    if (typeof require !== "undefined") {
           //
           //
        } else {
            //Create head tag dynamically
            var head = document.getElementsByTagName('head')[0];

            //Create link or script tag dynamically
            var link = document.createElement('link');

            //Add appropriate attributes
            link.href = "/resource/bootstrap"; 
            link.rel = "stylesheet";

            head.appendChild(link);
        } 

},//Delimiter for future code

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.