2 回答

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超7個(gè)贊
這似乎按預(yù)期工作:
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);

TA貢獻(xiàn)1880條經(jīng)驗(yàn) 獲得超4個(gè)贊
嘗試這個(gè):
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');
}
});
- 2 回答
- 0 關(guān)注
- 209 瀏覽
添加回答
舉報(bào)