", "upvoteCount": 0, "url": "https://trailhead.salesforce.com/trailblazer-community/feed/0D54V00007FSdeQSAT", "datePublished": "2023-08-31T07:18:12.000Z", "author": { "@type": "Person", "name": "Tushar Jadav", "url": "https://trailblazers.salesforce.com/profileView?u=0053A00000EdUHHQA3", "affiliation": { "@type": "Organization", "name": "Biztech IT Consultancy PVT LTD" } } } ] } } Skip to main content

Hello,

 

I would like to use jQuery in a LWC component in the Salesforce1 mobile app, however I am encountering difficulties when trying to load the resource.

 

What I have done:

 

I have uploaded the jQuery as a static resource.

 

I import the static resource in the LWC component:

 

import jquery214 from '@salesforce/resourceUrl/QUEST_psd3_graph';

Then I load the resource in the renderedCallback:

 

Promise.all([            loadScript(this, jquery214 + '/jquery-2.1.4.min.js')        ]).then(() => {            getData({recordId: this.recordId})                .then((result) => {                    console.log('Libraries loaded!');                    if (typeof jQuery == 'undefined') {                        console.log('JQuery not loaded!');                    } else {                        console.log('JQuery loaded!');                    }                }).catch(error => {                    console.log(error.message);                    console.log(error.stack);                });        })

Everything works as expected on the desktop version. JQuery loads and I am able to use it. When I open the same app on the Salesforce1 mobile app, JQuery is not loaded and I cannot use it. The "Libraries loaded" is successfully logged, as well as JQuery not loaded since typeof jQuery is undefined. No errors are thrown.

 

I would greatly appreciate any assistance.

 

Best regards

 

@* Salesforce Developers *

4 respostas
  1. 31 de ago. de 2023, 07:18

    Hello @Timotej Brumec,

    I have researched and found that the reason why jQuery is not loaded on the Salesforce1 mobile app is that the loadScript function from the platformResourceLoader module does not work properly on mobile devices. The loadScript function is used to import external JavaScript libraries such as jQuery in LWC components. However, on mobile devices, the function may fail to load the script or load it after the component is rendered, resulting in errors or unexpected behaviours.

     

    One possible solution is to use a static resource to store the jQuery library file and use the afterScriptsLoaded attribute to ensure that the script is loaded before the component is rendered. You can also use the ltng:require tag instead of the loadScript function to import the script. For example, you can write something like this:

     

    <apex:page>

    <apex:includeLightning />

    <div id="lightning" />

    <script>

    $Lightning.use("c:myApp", function() {

    $Lightning.createComponent("c:myComponent",

    {},

    "lightning",

    function(cmp) {

    // do some stuff

    });

    });

    </script>

    </apex:page>

    <aura:application access="GLOBAL" extends="ltng:outApp">

    <aura:dependency resource="c:myComponent"/>

    </aura:application>

    <aura:component implements="force:appHostable">

    <ltng:require scripts="{!$Resource.jquery}" afterScriptsLoaded="{!c.doInit}" />

    <!-- rest of the component -->

    </aura:component>