Javascript-条件在Android上总是返回0

Javascript-条件在Android上总是返回0,javascript,android,sencha-touch,Javascript,Android,Sencha Touch,我刚刚经历了最奇怪的事情。我正在使用sencha开发我的应用程序,当应用程序加载body onload=doStuff时,我会做一个测试来检测设备的方向,我会这样做: var bodyWidth = document.body.clientWidth; var bodyHeight = document.body.clientHeight; var orientation = bodyHeight > bodyWidth; 警报方向: 在桌面上:它显示为真 在Android上:它显示0,

我刚刚经历了最奇怪的事情。我正在使用sencha开发我的应用程序,当应用程序加载body onload=doStuff时,我会做一个测试来检测设备的方向,我会这样做:

var bodyWidth = document.body.clientWidth;
var bodyHeight = document.body.clientHeight;
var orientation = bodyHeight > bodyWidth;
警报方向:

在桌面上:它显示为真 在Android上:它显示0,这意味着错误,无论设备的方向如何,这种情况都会发生 有人能解释一下吗


编辑:bodyHeight和bodyWidth都有正确的值。即使alertbodyHeight>bodyWidth在控件中显示为true,也可以捕获方向更改事件,如下所示:

viewport: {
   //capture the orientation change event
   orientationchange: 'onOrientationchange'
 }

onOrientationchange: function(viewport, orientation) {
    dostuff() // based on orientation
}
除此之外,您还应该尝试window.innerHeight和window.innerWidth

涵盖所有浏览器的实用JavaScript解决方案:

var w = window.innerWidth
|| document.documentElement.clientWidth
|| document.body.clientWidth;

var h = window.innerHeight
|| document.documentElement.clientHeight
|| document.body.clientHeight;

最终发现了问题所在:在Android浏览器中,方向是一个关键词。更改变量名解决了问题。

因此alertbodyHeight>bodyWidth显示为true,而alertorientation显示为0 false?奇怪..你用sencha touch关键字发布了这个。所以为什么不使用这个:它不能回答你的问题,但它是有效的。这就是我最后所做的。谢谢