如何检测设备的屏幕分辨率并确定它是台式机还是移动设备?

时间:2016-04-07 16:06:56

标签: javascript

我的网站存在问题 - 基本上一切都在1080p(或更好)上工作正常,但我最近在非1080p质量的计算机上访问过网站,而且看起来并不是最好的。我一直试图弄清楚如何确定访问网站的设备的分辨率,如果它们没有足够好的分辨率,则会显示alert消息。

我下面的代码确实有用,但问题是它仍然会在我的iPhone上显示警告消息,因为用户体验在我的iPhone上运行得很好(6)。我只想在设备上的分辨率很差时(例如我正在使用的笔记本电脑)显示alert

以下是我尝试的所有内容,都给了我相同的结果(显示alert,但也在我的iPhone上显示):

    var width = document.documentElement.clientWidth;
    var height = document.documentElement.clientHeight;
    if (height <= 781 || width <= 781) {
        alert("Sorry, you may not have the best experience with the screen resolution you are using.")
    }

    var width = $(window).width();
    var height = $(window).height();
    if (height <= 781 || width <= 781) {
        alert("Sorry, you may not have the best experience with the screen resolution you are using.")
    }

    var width = screen.width;
    var height = screen.height;
    if (height <= 781 || width <= 781) {
        alert("Sorry, you may not have the best experience with the screen resolution you are using.")
    }

我发现我的问题是我的iPhone的屏幕widthheight小于781,但是如果它是好的&#34我怎么能破译? ;分辨率,当它是&#34;坏&#34;如果这是有道理的。

在搜索网络后,我仍然不确定我还需要做些什么才能让它在移动设备上显示而不是。非常感谢任何指导。

1 个答案:

答案 0 :(得分:0)

最好检测此问题的设备类型。

您可以使用此代码段来检测设备

if( /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
 // some code..
}
相关问题