使用 window.open 从主页面创建弹出窗口,子/弹出页面使用 onload 事件,该事件应显示其宽度/高度。然而在 Chrome 中,这将始终导致 0。检查窗口和文档的宽度/高度(无论是否使用 jQuery)都只会返回 0。奇怪的是,在选项卡中加载子页面本身将始终正确报告宽度/高度。
这是正确的行为吗? load事件有问题吗?使用 Chrome 浏览器?是否有另一个事件我可以等待以确保设置宽度/高度属性?
如果在加载中设置超时,您可能会得到正确的值。但这似乎只是一个关于在检查宽度/高度之前等待多长时间的猜测游戏。是否没有办法等待特定事件来确定在 Chrome 中设置了子窗口的宽度/高度?
下面是两个页面的快速演示。希望有人可能有一些解决方案,这不涉及猜测放入 setTimeout 的不确定时间。
编辑:忘了提及,我已经在 Chrome 版本 14/15 上对此进行了测试。
main.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>main</title>
</head>
<body>
<input type="button" value="Pop" onclick="window.open('http://localhost:8085/social_test/test.html', 'namedWnd', 'width=320,height=240')" />
</body>
</html>
测试.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN"
"http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<title>test</title>
<script type="text/javascript" src="jquery-1.6.4.js"></script>
<script type="text/javascript">
$(window).load(function(){
var report = function() {
alert($(window).width() + " x " + $(window).height());
};
report();
//window.setTimeout(report, 1000);
});
// without any jquery
/*window.onload = function() {
var report = function() {
alert(window.innerWidth + " x " + window.innerHeight);
// or document's size
//alert(window.document.documentElement.clientWidth
// + " x " + window.document.documentElement.clientHeight);
};
report();
//window.setTimeout(report, 1000);
};*/
</script>
</head>
<body/>
</html>
最佳答案
为什么不使用 $(document) 而不是 $(window) : http://api.jquery.com/height/
我自己在一个流行应用程序中使用过它,它可以工作,没有超时或任何问题
关于javascript - Chrome 中的 jQuery 加载事件在弹出窗口中返回零的宽度/高度值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8058958/