javascript - 移动设备访问检测

标签 javascript html css mobile

<分区>

我正在开发一个网站,我计划同时发布一个配套应用程序,因为它在移动设备上看起来不如在桌面设备上好。还有很多性能问题。我注意到如果您使用的是移动设备,某些网站会通知您转到不同的页面,我想做类似的事情,只是弹出一条消息。我确定我可以处理消息部分,但我需要帮助的是检测移动设备使用情况背后的方法。我通过简单的谷歌搜索看到了这个网站:

http://detectmobilebrowsers.com/

但我完全不知道如何用 JS 实现它。

提前感谢您的帮助!

最佳答案

试试这个

/**
    * Mobile Detect
    * @license    http://www.opensource.org/licenses/mit-license.php The MIT License
*/
class Mobile_Detect
{
    protected $accept;
    protected $userAgent;
    protected $isMobile = false;
    protected $isAndroid = null;
    protected $isAndroidtablet = null;
    protected $isIphone = null;
    protected $isIpad = null;
    protected $isBlackberry = null;
    protected $isBlackberrytablet = null;
    protected $isOpera = null;
    protected $isPalm = null;
    protected $isWindows = null;
    protected $isWindowsphone = null;
    protected $isGeneric = null;
    protected $devices = array(
    "android" => "android.*mobile",
    "androidtablet" => "android(?!.*mobile)",
    "blackberry" => "blackberry",
    "blackberrytablet" => "rim tablet os",
    "iphone" => "(iphone|ipod)",
    "ipad" => "(ipad)",
    "palm" => "(avantgo|blazer|elaine|hiptop|palm|plucker|xiino)",
    "windows" => "windows ce; (iemobile|ppc|smartphone)",
    "windowsphone" => "windows phone os",
    "generic" => "(kindle|mobile|mmp|midp|pocket|psp|symbian|smartphone|treo|up.browser|up.link|vodafone|wap|opera mini)");

    public function __construct()
    {
        $this->userAgent = $_SERVER['HTTP_USER_AGENT'];
        $this->accept = $_SERVER['HTTP_ACCEPT'];

        if (isset($_SERVER['HTTP_X_WAP_PROFILE']) || isset($_SERVER['HTTP_PROFILE']))
        {
            $this->isMobile = true;
        }
        elseif (strpos($this->accept, 'text/vnd.wap.wml') > 0 || strpos($this->accept, 'application/vnd.wap.xhtml+xml') > 0)
        {
            $this->isMobile = true;
        }
        else
        {
            foreach ($this->devices as $device => $regexp)
            {
                if ($this->isDevice($device))
                {
                    $this->isMobile = true;
                }
            }
        }
    }

    /**
        * Overloads isAndroid() | isAndroidtablet() | isIphone() | isIpad() | isBlackberry() | isBlackberrytablet() | isPalm() | isWindowsphone() | isWindows() | isGeneric() through isDevice()
        *
        * @param string $name
        * @param array $arguments
        * @return bool
    */
    public function __call($name, $arguments)
    {
        $device = substr($name, 2);
        if ($name == "is" . ucfirst($device) && array_key_exists(strtolower($device), $this->devices))
        {
            return $this->isDevice($device);
        } 
        else
        {
            trigger_error("Method $name not defined", E_USER_WARNING);
        }
    }

    /**
        * Returns true if any type of mobile device detected, including special ones
        * @return bool
    */
    public function isMobile()
    {
        return $this->isMobile;
    }

    protected function isDevice($device)
    {
        $var = "is" . ucfirst($device);
        $return = $this->$var === null ? (bool) preg_match("/" . $this->devices[strtolower($device)] . "/i", $this->userAgent) : $this->$var;
        if ($device != 'generic' && $return == true) {
            $this->isGeneric = false;
        }
        return $return;
    }
}   

   //call this way
    $detect= new Mobile_Detect();
    if ($detect->isMobile())
    {
        $_SESSION['mobile']="mobile";
    }

关于javascript - 移动设备访问检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27684124/

上一篇:html - Div 应该根据屏幕尺寸增长和收缩吗?

下一篇:javascript - 移动菜单 Accordion 逻辑

相关文章:

javascript - 通过JS点击所有按钮

javascript - 在 <td> 标签中打断单词

javascript - 如何使用javascript删除复选框

css - Tumblr:如何使 "Previous Page"按钮在第一个站点上可见?

javascript - Math.floor() 不适合我

javascript - 如何在暴露公共(public)静态方法时在 Javascript 中使用 'hide' 变量?

html - 两个 iframe 并排响应?

html - CSS - 网格行居中

css - IE7悬停下拉菜单问题

javascript - 如何获取 iframe 的innerHTML?