简体   繁体   English

JavaScript:如何检查单个源中包含的多个库是否已加载?

[英]JavaScript: How to check if multiple libraries contained in a single source has loaded?

I am faced with a multipleDefine error when my external JavaScript library(which loads multiple other external libraries) clashes with <script src="https://js.arcgis.com/4.8/"></script> . 当我的外部JavaScript库(加载多个其他外部库)与<script src="https://js.arcgis.com/4.8/"></script>冲突时,我遇到了multipleDefine错误。 I have tried moving the Esri script to the bottom most part of the code while my external library stays on top and it didn't work as it takes awhile to load. 我尝试将Esri脚本移至代码的最底部,而我的外部库仍位于顶部,并且该加载程序需要一段时间才能正常工作。

Is there a way that would allow me to check if the multiple libraries under my external library has loaded before I start loading the Esri script? 有没有一种方法可以让我在开始加载Esri脚本之前检查外部库下的多个库是否已加载?

Note: The Esri script is not required to be loaded immediately, it could be as late as needed. 注意:不需要立即加载Esri脚本,它可以根据需要延迟加载。

You can use load event(which ensures all your external files are loaded) and add your Esri script dynamically, once all external files are loaded. 您可以使用load事件(确保所有external files都已加载),并在所有外部文件加载后动态添加Esri script

See - https://javascript.info/onload-ondomcontentloaded 参见-https://javascript.info/onload-ondomcontentloaded

load – not only HTML is loaded, but also all the external resources: images, styles etc. 加载–不仅加载HTML,还加载所有外部资源:图像,样式等。

window.addEventListener('load', function() {
    const script = document.createElement('script');
    script.src = "https://js.arcgis.com/4.8/";
    document.querySelector('body').appendChild(script);
});

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

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