IFrame 中的动态脚本不触发 window.onload
我动态创建一个 IFrame
,然后向其中添加一个 script
标记,其中包含应该在框架加载时执行的代码,但它永远不会执行。
$stage.html("<iframe src='javascript:;' id='frame'></iframe>");
$frame = $("#frame");
frame = $frame[0].contentDocument ? $frame[0].contentDocument : $frame[0].contentWindow.document;
script= frame.createElement('script'),
head = frame.getElementsByTagName("head")[0];
script.innerHTML = "window.onload = function() { alert('loaded'); }";
head.appendChild(script);
我假设在将代码添加到 IFrame
之前触发 window onload
事件。有什么办法可以确保它被调用吗?此外,它还需要包含在 window.onload
中,因为它是基于 JS 的代码编辑器,因此 iframe 必须像在浏览器窗口中一样做出反应。干杯。
I am dynamically creating an IFrame
then adding a script
tag to it with code that should execute when the frame loads, however it is never executed.
$stage.html("<iframe src='javascript:;' id='frame'></iframe>");
$frame = $("#frame");
frame = $frame[0].contentDocument ? $frame[0].contentDocument : $frame[0].contentWindow.document;
script= frame.createElement('script'),
head = frame.getElementsByTagName("head")[0];
script.innerHTML = "window.onload = function() { alert('loaded'); }";
head.appendChild(script);
I am assuming the window onload
event is triggered before the code is added to the IFrame
. Is there anyway to ensure it is called? Also it needs to be contained with in window.onload
as it is for a JS based code editor so the iframe has to react as it would in a browser window. Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过使用
window.write()
解决。Solved by using
window.write()
.