Javascript来检测手机浏览器的屏幕宽度?宽度、屏幕、浏览器、手机

2023-09-05 02:16:16 作者:勉强可爱

我用screen.width获取浏览器的宽度。它的罚款与iPhone Safari浏览器,但是在android手机没有工作(这表明800宽度的Andr​​oid浏览器)。

i used screen.width to get the width of the browser. it worked fine with iphone safari but didn't work in android phone(it showed 800 width for android browser).

有没有兼容的方式来获取屏幕宽度。我不想使用用户代理,以检查它的移动浏览器。想使用JavaScript为和逻辑应包括屏幕宽度。

Is there any compatible way to get the screen width. I dont want to use UserAgent to check if its mobile browser. would like to use a javascript for that and logic should include screen width.

推荐答案

这是认识问题 - 看这里

It's know issue - see here

在第一次加载页面的 screen.width screen.height 是错误的。 尝试这样的超时:

When page first loads the screen.width and screen.height are wrong. Try a timeout like this:

setTimeout(CheckResolution, 300);

其中, CheckResolution 是函数。