gpt4 book ai didi

javascript - 调用 document.close 后 Document.links 为空

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

当您使用 Firefox 3 执行以下示例时:

<html>
<head>
<script language="javascript" type="text/javascript">
<!--
function openWindow(){
var w = window.open('', 'otherWin', 'width=600,height=600');
w.document.write(document.getElementsByTagName("html")[0].innerHTML);
w.document.close();
reportLinks(w.document.links);
}

function reportLinks(links){
var report = 'links: '+links.length;
for (var i=0;i<links.length;i++){
report += '\n (link='+links[i].href+')';
}
alert(report);
}
//-->
</script>
</head>
<body>
<p><a href="javascript: openWindow()">Open Same Content and Show Links Report</a></p>
<p><a href="javascript: reportLinks(document.links)">Show Links Report</a></p>
</body>
</html>

您将看到,单击“显示链接报告”时显示的链接数量与单击“打开相同内容并显示链接报告”时显示的链接数量均为 2。但是,当从此页面引用外部 JavaScript 文件时,行为似乎有所不同(如果需要,只需创建一个空文件 some.js )。单击“打开相同内容并显示链接报告”时,链接数量将为 0。

<html>
<head>
<script language="javascript" type="text/javascript" src="some.js"></script>
<script language="javascript" type="text/javascript">
<!--
function openWindow(){
var w = window.open('', 'otherWin', 'width=600,height=600');
w.document.write(document.getElementsByTagName("html")[0].innerHTML);
w.document.close();
reportLinks(w.document.links);
}

function reportLinks(links){
var report = 'links: '+links.length;
for (var i=0;i<links.length;i++){
report += '\n (link='+links[i].href+')';
}
alert(report);
}
//-->
</script>
</head>
<body>
<p><a href="javascript: openWindow()">Open Same Content and Show Links Report</a></p>
<p><a href="javascript: reportLinks(document.links)">Show Links Report</a></p>
</body>
</html>

这可能是加载页面和reportLinks 准确执行的时刻的问题。我假设添加了外部 some.js,该文档尚未完全构建。有没有办法可以为 onload 事件注册此 reportLinks 调用,以便我可以确定 document.links 是否完整?

顺便说一句,该示例在两种情况下都可以在 Google Chrome 中正常运行。

(在答案1之后添加)

按照 Marcel K 的建议。我重写了示例,还按照我真正希望的方式添加了代码。现在对其进行测试,这个简单的示例似乎适用于 Firefox 和 Chrome。

<html>

<head>

<script type="text/javascript" src="some.js"></script>
<script type="text/javascript">
<!--
function openWindow(){
var w = window.open('', 'otherWin', 'width=600,height=600');
w.document.write('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">\n<html>\n'+
document.getElementsByTagName("html")[0].innerHTML+'\n</html>');
w.onload=function(){
reportLinks(w.document.links);
};
w.document.close();
}

function reportLinks(links){
var report = 'links: '+links.length;
for (var i=0;i<links.length;i++){
report += '\n (link='+links[i].href+')';
}
alert(report);
}
//-->
</script>
</head>
<body>
<p><a href="javascript: openWindow()">Open Same Content and Show Links Report</a></p>
<p><a href="javascript: reportLinks(document.links)">Show Links Report</a></p>
</body>
</html>

我希望通过这个简单的示例来展示我正在编写的实际代码的简单情况。复杂 html 的打印预览屏幕,我想在打开后禁用所有 href。但在这种情况下,永远不会调用 onload 处理程序...在这种情况下,我如何以最可靠的方式注册 onload 处理程序?

非常感谢,马塞尔

最佳答案

正如我在评论中所说,这是一个非常奇怪的问题。但我认为发生这种情况是因为包含外部脚本会导致(新页面)页面渲染延迟,并且其 DOM 可能尚未准备好检查。

我的怀疑得到了添加(新)defer 的事实的支持。属性似乎可以解决这个问题:

This Boolean attribute is set to indicate to a browser that the script is meant to be executed after the document has been parsed.

可以在原始页面上设置 defer 属性,因为您想要它的精确副本。如果包含脚本的位置并不重要(例如,在包含的文件中使用 document.write 时,包含在哪个位置确实),您可以设置它它)。

由于defer是一个 bool 属性,it is activated当它只是存在时 (defer) 或(使用 XHTML 时)设置为自身 (defer="defer")。在您的情况下,脚本包含内容如下:

<script type="text/javascript" src="some.js" defer></script>
<小时/>

更新关于您的更新:您仍然应该在主页中插入文档类型(考虑使用 HTML 5 one )。

我认为您附加 onload 事件的方式是最好的。

但考虑到您想要实现的目标(没有超链接的打印预览):您也可以使用“打印”media attribute以及像文本一样设置超链接的样式;这比您正在做的事情方式更容易,并且在禁用 JavaScript 时它可以工作。

关于javascript - 调用 document.close 后 Document.links 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3591477/

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