For smart Primates & ROBOTS (oh and ALIENS ).

Monday, April 15, 2024

Add Dependent Scripts

 

 here is the code that allow to add dependent script :

 

 

    <script>
        const addDependentScripts = async function( scriptsToAdd ) {
        const s=document.createElement('script')
        for ( var i = 0; i < scriptsToAdd.length; i++ ) {
            let r = await fetch( scriptsToAdd[i] )
            s.text += await r.text()
        }
        document.querySelector('body').appendChild(s)
        }
         </script> 


Uses as below :

       <script>

    try {
        addDependentScripts( [
            "https://ajax.googleapis.com/ajax/libs/jquery/2.2.4/jquery.min.js",
            "https://cdn.mywebshopapp.com/test/a1.js",
            "https://cdn.mywebshopapp.com/test/a2.js",
            "https://cdn.mywebshopapp.com/test/a3.js",
            "https://cdn.mywebshopapp.com/test/a4.js"
        ] );
        }
        catch (err) { }

       </script>

Share:

Featured Post

Core Web Vitals Assessment showing LCP 10.6s while google pagespeed report showing LCP 3.5s why?

Core Web Vitals Assessment showing LCP 10.6s while google pagespeed report showing LCP 3.5s why? So, you can face this situation while...

Ads Inside Post

Powered by Blogger.

Arsip