我正尝试在一个表单上运行javascript,该表单加载了来自外部js文件的内容,但由于内容不能立即加载而失败
我尝试过监听iframe中html按钮的加载,但没有得到任何结果:
document.querySelector('#hyform button').addEventListener('load', function() {
console.log('external js content loaded');
}
更新了问题。这不是iframe,而是从外部js文件加载的内容。我不能控制外部的js文件。
发布于 2018-06-08 04:15:30
您可以动态地将文件追加到<head>
,然后监控加载状态:
var callback = function () {
// do stuff
};
var script = document.createElement('script');
script.src = 'path_to_file.js'
script.type = 'text/javascript';
if (script.readyState) {
if (script.readyState === 'loaded' || script.readyState === 'complete') {
callback();
script.onreadystatechange = null;
}
} else {
script.onload = function () {
callback();
};
}
document.getElementsByTagName("head")[0].appendChild(script);
https://stackoverflow.com/questions/50749289
复制相似问题