I tried to load some scripts into a page using innerHTML
on a <div>
.
(我试图在<div>
上使用innerHTML
将一些脚本加载到页面中。)
It appears that the script loads into the DOM, but it is never executed (at least in Firefox and Chrome).(脚本似乎已加载到DOM中,但从未执行(至少在Firefox和Chrome中)。)
Is there a way to have scripts execute when inserting them with innerHTML
?(使用innerHTML
插入脚本时,是否可以执行脚本?)
Sample code:
(样例代码:)
<!DOCTYPE html> <html> <body onload="document.getElementById('loader').innerHTML = '<script>alert(\'hi\')<\/script>'"> Shouldn't an alert saying 'hi' appear? <div id="loader"></div> </body> </html>
ask by Craig translate from so
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…