简体   繁体   English

页面加载后如何调用库脚本?

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

I'm trying to call a library script (locomotive) in vanilla JS without Jquery, after the whole page and its assets had loaded.在整个页面及其资产加载后,我试图在没有 Jquery 的香草 JS 中调用库脚本(机车)。 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?还有什么我可以尝试的吗?

Your answer may be how to dynamic import script in js.你的答案可能是如何在 js 中动态导入脚本。 the base way is that insert a tag by dom api.基本方法是通过 dom api 插入标签。

eg例如

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

if you wanna more functions, plz try loadjs如果您想要更多功能,请尝试loadjs

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM