gpt4 book ai didi

Javascript iframe 缓存问题

转载 作者:行者123 更新时间:2023-11-28 10:33:07 25 4
gpt4 key购买 nike

我有一个 iframe,它通过 javascript 加载一些 javascript。仅在 Internet Explorer 中,当且仅当 IE 具有 javascript 的缓存副本时,此操作才有效。这意味着重新加载页面(或者以其他方式触发脚本再次运行,如果它卡在函数或其他什么中)将导致此代码工作,否则不会。特别是,document.write 调用失败。

主页:

<iframe height = "200" width = "200" id = "happy">
</iframe>
<script type="text/javascript">
var a = document.getElementById("happy");
scripttxt = '<a href="#" id="joy">JOY</a><'+'script type="text/javascript" src="fail.js"></'+'script>';
a.src = "about:blank";
a.contentWindow.document.open();
a.contentWindow.document.write("<h3>Preview:</h3>" + scripttxt + "");
a.contentWindow.document.close();
</script>

失败.js:

document.write(document.getElementById("joy"));

我意识到我可以使用条件注释让 IE 跳过主页脚本中的 document.open()document.close(),但让 IE 跳过document.open() 和 document.close() 感觉有点老套(编辑)...并且破坏了 IE 中的其他功能

编辑:当它正常工作时,iframe 将在预览标题下包含文本:JOYhttp://mymachine:myport/mainpage.htm#,其中 JOY 是一个超链接。当失败时,它将省略 http://mymachine:myport/mainpage.htm# 。我实际上并不关心 document.write 如何处理写入 a 节点,只是它能够成功获取元素并成功写入内容。

在 Justin 的建议下,我也尝试了这个版本的脚本,但它的行为完全相同:

var a = document.getElementById("happy");
a.src = "about:blank";
r = document.createElement("a");
r.id="joy";
r.href="#";
r.innerText="JOY";
s = document.createElement("script");
s.src="fail.js";
a.contentWindow.document.body.appendChild(r);
a.contentWindow.document.body.appendChild(s);

最佳答案

为什么使用document.write?相反,请尝试附加脚本节点。

var d = document.getElementById("happy"),
s = document.createElement("script");

s.src= "http://asdf.com/asdf.js";
d.contentWindow.document.body.appendChild(s);

关于Javascript iframe 缓存问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2465805/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com