gpt4 book ai didi

具有动态内容和空白 url 的 JavaScript 新窗口,未调用事件监听器

转载 作者:行者123 更新时间:2023-12-03 05:03:06 28 4
gpt4 key购买 nike

案例1:

var printWindow = window.open("", "print_window");
if(printWindow) {

printWindow.addEventListener('load', function(){alert(1);}, false); //Everything works but this listener is never called

printWindow.document.write(printView);
printWindow.focus();
}

案例2:

var printWindow = window.open("<html>useless random stuff</html>", "print_window");
if(printWindow) {

printWindow.addEventListener('load', function(){alert(1);}, false); //this is hit
printWindow.focus();
}

那么,动态加载 HTML 到新窗口文档会取消加载事件监听器吗?还是我真的把事情搞砸了?
需要帮助:D

最佳答案

您遇到一些文档上下文问题。查看documentationdummy examples对于 window.open

还要检查下面的代码片段,并注意如何从不同的窗口命名空间调用警报函数。

<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<button id="window_opener">Click me to load a new window</button>
<script type="text/javascript">
document.getElementById("window_opener").addEventListener('click', function(){
var badDocumentString = "<html><head><title>print window</title></head><body>I live in child window</body></html>";
var child = window.open()
childdoc = child.document
childdoc.write(badDocumentString);
childdoc.close();
child.addEventListener("load", function(){
child.alert("zeee germans"); //attention here
});
})
</script>
</body>
</html>

关于具有动态内容和空白 url 的 JavaScript 新窗口,未调用事件监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42130817/

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