我如何等待附加到正文的 <script> 完成执行
How can I wait for <script> appended to body to finish executing
当用户单击一个按钮时,我需要将一个外部脚本附加到我的正文中。这是要求。
菜鸟问题。假设外部库创建了一个 window.newLib 变量。当 newLib 变量可供我使用时如何通知我?
我试过了但是没有调用 onload:
const script = document.createElement('script');
script.innerHTML = 'window.test = 1; console.log("test defined.");';
script.onload = "console.log('onload called.', window.test)"
script.async = false;
document.body.appendChild(script);
这行得通,但我觉得它很脏。有没有更好的方法?
const injectScript = () => {
const script = document.createElement('script');
script.innerHTML = 'setTimeout(() => {window.test = 1},10500); console.log("test defined.");';
script.async = false;
document.body.appendChild(script);
}
const nap = ms => new Promise(res => setTimeout(res, ms));
const maxAttempts = 100;
const msNap = 100; // 10s timeout
const start = async () => {
injectScript();
let id = 0;
while (true) {
if (++id === maxAttempts) {
throw(`Lib took too long to load: ${id * msNap}ms`);
}
if (window.test) break;
await nap(msNap);
}
console.log('External lib correctly loaded.');
};
start();
感谢@Ultimater。这似乎按预期工作:
const script = document.createElement('script');
script.src = 'data:text/html,id = 0;while(true){ if(++id==1000000) break;} window.test = 1; console.log("test defined.");';
script.onload = () => console.log('onload called.', window.test);
document.body.appendChild(script);
试试这个:
const injectScript = () => {
const script = document.createElement('script');
script.innerHTML = 'console.log("Function loaded..."); window.postMessage({cmd:"loaded"});';
script.async = false;
document.body.appendChild(script);
}
setTimeout(injectScript, 3000);
window.addEventListener('message', function(e){
if(e.data.cmd === 'loaded'){
console.log('external library loaded');
}
});
当用户单击一个按钮时,我需要将一个外部脚本附加到我的正文中。这是要求。
菜鸟问题。假设外部库创建了一个 window.newLib 变量。当 newLib 变量可供我使用时如何通知我?
我试过了但是没有调用 onload:
const script = document.createElement('script');
script.innerHTML = 'window.test = 1; console.log("test defined.");';
script.onload = "console.log('onload called.', window.test)"
script.async = false;
document.body.appendChild(script);
这行得通,但我觉得它很脏。有没有更好的方法?
const injectScript = () => {
const script = document.createElement('script');
script.innerHTML = 'setTimeout(() => {window.test = 1},10500); console.log("test defined.");';
script.async = false;
document.body.appendChild(script);
}
const nap = ms => new Promise(res => setTimeout(res, ms));
const maxAttempts = 100;
const msNap = 100; // 10s timeout
const start = async () => {
injectScript();
let id = 0;
while (true) {
if (++id === maxAttempts) {
throw(`Lib took too long to load: ${id * msNap}ms`);
}
if (window.test) break;
await nap(msNap);
}
console.log('External lib correctly loaded.');
};
start();
感谢@Ultimater。这似乎按预期工作:
const script = document.createElement('script');
script.src = 'data:text/html,id = 0;while(true){ if(++id==1000000) break;} window.test = 1; console.log("test defined.");';
script.onload = () => console.log('onload called.', window.test);
document.body.appendChild(script);
试试这个:
const injectScript = () => {
const script = document.createElement('script');
script.innerHTML = 'console.log("Function loaded..."); window.postMessage({cmd:"loaded"});';
script.async = false;
document.body.appendChild(script);
}
setTimeout(injectScript, 3000);
window.addEventListener('message', function(e){
if(e.data.cmd === 'loaded'){
console.log('external library loaded');
}
});