How can I call a library script after page is loaded?

119 Views Asked by At

I'm trying to call a library script (locomotive) in vanilla JS without Jquery, after the whole page and its assets had loaded. I've already tried with something like this:

<body onload="script('./js/lib/locomotive.min.js')";>

But it isn't working, the whole page loads but the script is never called. I've also tried placing the defer after the scripts call, but still nothing. I saw this other solution in other comments:

document.addEventListener("DOMContentLoaded", function(){

    //....
    //  **But I don't know how to call in here the 
      *./js/lib/locomotive.min.js*** 

});

Is there something else I could try?

1

There are 1 best solutions below

0
X-Ray On

Your answer may be how to dynamic import script in js. the base way is that insert a tag by dom api.

e.g.

const script = document.createElement('script')
script.src = './js/lib/locomotive.min.js'
document.head.appendChild(script)

if you wanna more functions, plz try loadjs