【发布时间】:2011-11-09 00:26:49
【问题描述】:
使用 window.open 从主页面创建一个弹出窗口,子/弹出页面使用 onload 事件,该事件应该显示它的宽度/高度。然而,在 Chrome 中,这将始终导致 0。检查窗口和文档的宽度/高度(使用或不使用 jQuery)只会返回 0。奇怪的是,在选项卡中加载子页面本身总是会正确报告宽度/高度。
这是正确的行为吗?加载事件有问题吗?与铬?我可以等待另一个事件来确保特别设置宽度/高度属性吗?
如果在 onload 中设置了超时,您可能会得到正确的值。但这似乎只是一个关于在检查宽度/高度之前等待多长时间的猜谜游戏。有没有办法等待特定事件确定子窗口的宽度/高度在 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>
test.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>
【问题讨论】:
-
如果在正文中添加一些内容呢?
-
您的代码在 Chrome 16 中对我来说基本上是按原样工作的——显示正确的尺寸。
-
只要我检查文档的宽度/高度,这实际上似乎有效。
-
@Nicholi -- 实际上从头开始,我认为现在有一个问题。也许是一个 jquery 错误,但不确定。
-
@david 是否仅在您第一次打开弹出窗口时起作用?然后停止?我认为 roberkules 在这里一针见血。如果我在文档正文中放一些东西并检查 $(document).width()/height() 似乎没问题。至少在 Chrome 14/15 中。
标签: javascript jquery google-chrome cross-browser popup