1

Note: This will be reported to Google as a bug but fixes often get delayed.

A web page that calls a function based on a window's width works in IE, Firefox, etc. but "sometimes" fails in Chrome. The problem (in Chromium-based browsers) only occurred when the target web page automatically reloads upon browser startup.

<script>

var dde = document.documentElement;

if(!document.documentElement) dde = document.body; // fix for IE6 and earlier 

myWidth = Math.max(dde.scrollWidth,dde.offsetWidth,dde.clientWidth);

if(myWidth < 960) document.location.href="http://www.gooplusplus.com/mini.php";  

</script>
4

1 回答 1

0

进一步的测试发现,在浏览器启动时,即使窗口实际上已最大化,Chrome 也会从未最大化的窗口大小中获取其窗口宽度结果。此外,此错误仅发生在非活动浏览器选项卡上。

解决方案所需的两个步骤是检测:

(1) 浏览器是基于 Chromium 的吗?-->navigator.userAgent.indexOf("Chrome/")>0

(2) 选项卡是否处于非活动状态?-->document.webkitVisibilityState == "hidden"

测试网址:http ://www.gooplusplus.com/chrome-bug.html

我的部分解决方案:

<script>

var dde = document.documentElement;

var tabVisible = document.webkitVisibilityState;

if(!document.documentElement) dde = document.body; // fix for IE6 and earlier 

myWidth = Math.max(dde.scrollWidth,dde.offsetWidth,dde.clientWidth);

if( ( navigator.userAgent.indexOf("Chrome/")<0 || tabVisible!="hidden" ) && myWidth < 960 ) 
    document.location.href="http://www.gooplusplus.com/mini.php";  

</script>

这就是我基于窗口宽度的 URL 重定向所需的全部内容。但是,这绕过了另一个假设情况,其中 (1) 浏览器是 Chrome,(2) 选项卡处于非活动状态(隐藏),但 (3)最大尺寸确实 < 960 像素。那么,有没有更完整的解决方案呢?

于 2013-06-09T18:48:19.887 回答