检测移动浏览器屏幕宽度的Javascript?

检测移动浏览器屏幕宽度的Javascript?,javascript,android,resolution,Javascript,Android,Resolution,我使用screen.width获取浏览器的宽度。它在iphone safari上运行良好,但在android手机上不起作用(android浏览器显示800宽) 是否有任何兼容的方法来获得屏幕宽度。我不想使用UserAgent检查其移动浏览器是否可用。您希望使用javascript,并且逻辑应包括屏幕宽度。您可以尝试使用此url 或 函数getWidth() { xWidth=null; 如果(window.screen!=null) xWidth=window.screen.availWidt

我使用screen.width获取浏览器的宽度。它在iphone safari上运行良好,但在android手机上不起作用(android浏览器显示800宽)

是否有任何兼容的方法来获得屏幕宽度。我不想使用UserAgent检查其移动浏览器是否可用。您希望使用javascript,并且逻辑应包括屏幕宽度。

您可以尝试使用此url


函数getWidth()
{
xWidth=null;
如果(window.screen!=null)
xWidth=window.screen.availWidth;
如果(window.innerWidth!=null)
xWidth=window.innerWidth;
if(document.body!=null)
xWidth=document.body.clientWidth;
返回宽度;
}
函数getHeight(){
xHeight=null;
如果(window.screen!=null)
xHeight=window.screen.availHeight;
if(window.innerHeight!=null)
xHeight=window.innerHeight;
if(document.body!=null)
xHeight=document.body.clientHeight;
返回xhweight;
}
screen.height显示屏幕的高度
screen.width显示屏幕的宽度
screen.availHeight显示高度,但删除界面高度,如任务栏和浏览器菜单等。
screen.availWidth与上面相同,而是提供可用宽度
这是一个众所周知的问题-请参阅

当页面第一次加载
屏幕时,宽度
屏幕高度
是错误的。
请尝试这样的超时:

setTimeout(CheckResolution, 300);

其中,
CheckResolution
是您的功能。

只需阅读这两个URL即可。它会帮助你得到你需要的东西

仅供参考,Android可能有800像素宽。除其他外,请参阅本文:


编辑:哦,我认为另一个人指出Android bug比我更正确。

我发现使用
窗口。outerWidth
给出了正确的值。使用BrowserStack android模拟器对此进行了测试。

对于那些对获取浏览器宽度值感兴趣的人,我测试了几个选项:

我使用的是bootstrap 3,唯一一个将bootstrap 3断点与IE和Chrome匹配的解决方案是:

window.innerWidth 
以下是1200px窗口的结果:

Chrome

$( window ).width(): 1183
$( document ).width():1183
screen.width: 1536
$( window ).innerWidth():1183
$( document ).innerWidth():1183
window.innerWidth: 1200
$( document ).outerWidth():1183 
window.outerWidth: 1216
document.documentElement.clientWidth: 1183 
Internet Explorer 10

$( window ).width(): 1200
$( document ).width():1200
screen.width: 1536
$( window ).innerWidth():1200
$( document ).innerWidth():1200
window.innerWidth: 1200
$( document ).outerWidth():1200
window.outerWidth: 1214
document.documentElement.clientWidth: 1200

这只是返回整个窗口的宽度,包括chrome和所有内容。
$( window ).width(): 1200
$( document ).width():1200
screen.width: 1536
$( window ).innerWidth():1200
$( document ).innerWidth():1200
window.innerWidth: 1200
$( document ).outerWidth():1200
window.outerWidth: 1214
document.documentElement.clientWidth: 1200