gpt4 book ai didi

javascript - 在使用 window.open 打开的窗口中使用 document.write 编写的脚本元素不会在 Windows 7 的 IE8 中执行

转载 作者:数据小太阳 更新时间:2023-10-29 04:43:14 25 4
gpt4 key购买 nike

我遇到了一个似乎只出现在 Windows 7 上的问题。它似乎在不同版本的 Windows 上的 IE8 中运行良好。基本上,我使用 window.open() 创建一个新窗口,然后使用 document.write() 写入该新窗口的内容,其中包含脚本包含。在 IE 中,这些脚本未正确执行。大多数时候他们根本不执行,但偶尔会有一个执行。这仅适用于清除缓存 - 一旦 javascript 文件在缓存中,它就可以正常工作。

归结测试用例:

测试.html:

<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
<html>
<head>
<script type="text/javascript">
var w = window.open();
var windowHTML = "\
<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>\n\
<html>\n\
<head>\n\
<script type='text/javascript' src='test.js'></scr"+"ipt>\n\
<script type='text/javascript' src='test2.js'></scr"+"ipt>\n\
</head>\n\
<body>\n\
</body>\n\
</html>";
w.document.write(windowHTML);
w.document.close();
</script>
</head>
<body>
</body>
</html>

测试.js:

alert("test");

test2.js:

alert("test2");

当我转到 test.html 时,我希望看到一个新窗口弹出,其中包含“test”和“test2”的警报。我在大多数浏览器中都这样做,包括 IE6。但是,当我在 Windows 7 的 IE8 中尝试此操作时,它会打开空白页面,但不会出现任何警告(或偶尔只出现一个)。

这是某种时间问题吗?还有其他人看到这个吗?有什么办法可以解决这个问题吗?


编辑:这是 Rob Cooney 希望看到的我尝试过的代码。同样,它适用于其他浏览器,但不适用于 Windows 7 上的 IE8。

测试.htm:

<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
<html>
<head>
<script type="text/javascript">
var w = window.open();
var windowHTML =
"<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>\n" +
"<html>\n" +
"<head>\n" +
" <script type='text/javascript' src='test.js'></scr"+"ipt>\n" +
" <script type='text/javascript' src='test2.js'></scr"+"ipt>\n" +
"</head>\n" +
"<body onload='test();test2()'>\n" +
"</body>\n" +
"</html>";
w.document.write(windowHTML);
setTimeout(function() {
w.document.close();
}, 10000);
</script>
</head>
<body>
</body>
</html>

测试.js:

function test() {
alert("test");
}

test2.js:

function test2() {
alert("test2");
}

此外,我已将此作为问题发布在 MSDN 论坛上 here .


接受否的解决方法作为最佳答案。这是我修改后的代码:

<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>
<html>
<head>
<script type="text/javascript">
var w = window.open();
var windowHTML =
"<!DOCTYPE HTML PUBLIC '-//W3C//DTD HTML 4.01 Transitional//EN' 'http://www.w3.org/TR/html4/loose.dtd'>\n" +
"<html>\n" +
"<head></head>\n" +
"<body></body>\n" +
"</html>";
w.document.write(windowHTML);
w.document.close();

var s = w.document.createElement("script");
s.type = "text/javascript";
s.src = "test.js";
w.document.getElementsByTagName("HEAD")[0].appendChild(s);

var s2 = w.document.createElement("script");
s2.type = "text/javascript";
s2.src = "test2.js";
w.document.getElementsByTagName("HEAD")[0].appendChild(s2);
</script>
</head>
<body>
</body>
</html>

注意:此解决方案需要注意的是 javascript 文件现在是异步加载的,因此如果文件之间存在依赖关系,则无法确定它们是否会加载以正确的顺序。我使用 setTimeout 解决了这个问题,并在加载第二个文件之前测试了第一个文件中是否存在变量。

最佳答案

尝试这样的事情(未经测试):

var s=document.createElement('script');
s.type = "text/javascript";
s.src = "test.js";
document.body.appendChild(s);

关于javascript - 在使用 window.open 打开的窗口中使用 document.write 编写的脚本元素不会在 Windows 7 的 IE8 中执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3114202/

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