[英]how to load an external js file after a page loads and use a function from it
[英]how do i load an external js file after the page has loaded AND use a function from that same js
我正在尝试将webcam.js文件带入index.html文件,但需要在页面加载后加载。 它是一个网络摄像头文件,因此仅在索引文件加载后才打开。 我还必须能够使用该文件中的函数。 所以我的外部文件是webcam.js,我在index.html中使用了它,我还需要在html文件中使用来自webcam.js的函数showBlue()。
(index.html的下半部分)
<script src="C:/Users/Juniper/Documents/Apps/server/webcam.js" defer></script>
<script>
if (window.onload = showBlue) {
window.onload = function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", './screen.html', true);
xhttp.onreadystatechange = function() {
window.location.href = 'screen.html';
};
xhttp.send();
}
}
</script>
</body>
当前,这不会将屏幕更改为screen.html,但是如果我删除if语句(如果(window.onload = showBlue){),那么screen.html将自动加载
您将其包装在一个间隔计时器中,该计时器会定期检查脚本是否已加载。
var webcamInterval = setInterval( function() {
if ( someWellKnownFunctionNameFromWebcamJs ) {
clearInterval( webcamInterval );
// Your code dependent on webcam.js here ...
}
}, 100 );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.