一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

js/php 判断 手机操作系统/浏览器/微信浏览器 的代码

时间:2015-07-22 编辑:简简单单 来源:一聚教程网

js判断手机浏览器操作系统和微信浏览器的方法

做手机端的前端开发,少不了对手机平台的判断。如,对于app下载,就要判断在Android平台下就显示Android下载提示;在iOS平台下就显示iOS下载提示。

今天就为大家介绍一下用js判断手机客户端平台及系统平台的方法:

javascript">
//手机端判断各个平台浏览器及操作系统平台
function checkPlatform(){
if(/android/i.test(navigator.userAgent)){
document.write("This is Android'browser.");//这是Android平台下浏览器
}
if(/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)){
document.write("This is iOS'browser.");//这是iOS平台下浏览器
}
if(/Linux/i.test(navigator.userAgent)){
document.write("This is Linux'browser.");//这是Linux平台下浏览器
}
if(/Linux/i.test(navigator.platform)){
document.write("This is Linux operating system.");//这是Linux操作系统平台
}
if(/MicroMessenger/i.test(navigator.userAgent)){
document.write("This is MicroMessenger'browser.");//这是微信平台下浏览器
}
}
$(document).ready(function(){
alert(navigator.platform);
checkPlatform();
});


PHP 判断 操作系统 手机 浏览器 代码


一个蛋痛的项目,一个很龊的解决方案,配上一个完美的判断,这算什么,我无言,唉。不废话,看主题,今天和大家分享的是如果用PHP去判断当前浏览者的操作系统(包括手机和IPad等平板)和浏览器名称和版本号等。因为之前本人(Web编程技术交流网)也写过一些文章《PHP识别用户浏览器以及所使用的系统》《PHP判断浏览器类型的代码》《JS和PHP判断浏览器类型的方法【精华】》《JS判断浏览器类型》,今天把这些文件做一个总结并加强一下功能,把手机操作系统的判断也加进去,东西比较的全面,当然,这个东西,我不能保存他是永远正确的,因为各个浏览器的标准都是不一样的。所以,如果不正确的地方或者BUG大家可以一起交流一下,php判断浏览器,支持国内外多种浏览器,以及使用平台。相当给力…是一个php class。
主要涉及到以下几个方法(做一下简单的描述): 1.isBrowser($browserName)//判断是某浏览器,参数写浏览器的名字 2.getBrowser();//获得浏览器的名称 3.getPlatform();//获得操作系统的类型,如Windows或者LInux或者。。。 4.getVersion();//获得浏览器的版本号 5.getAolVersion();//获得Aol 6.isAol();//判断是否为AoL 7.isMobile();//判断是否为手机浏览器,自带的浏览器, 非第三方。 8.isRobot();//是否来自搜索引擎 9.getUserAgent();//… 10.isChromeFrame();//…
主要实现的几个方法,包含和器判断,操作系统的判断等 。


protected function checkBrowsers() {
    return (
        $this->checkBrowserWebTv() ||
        $this->checkBrowserInternetExplorer() ||
        $this->checkBrowserOpera() ||
        $this->checkBrowserGaleon() ||
        $this->checkBrowserNetscapeNavigator9Plus() ||
        $this->checkBrowserFirefox() ||
        $this->checkBrowserChrome() ||
        $this->checkBrowserOmniWeb() ||
        // common mobile
        $this->checkBrowserAndroid() ||
        $this->checkBrowseriPad() ||
        $this->checkBrowseriPod() ||
        $this->checkBrowseriPhone() ||
        $this->checkBrowserBlackBerry() ||
        $this->checkBrowserNokia() ||
        // common bots
        $this->checkBrowserGoogleBot() ||
        $this->checkBrowserMSNBot() ||
        $this->checkBrowserSlurp() ||
        // WebKit base check (post mobile and others)
        $this->checkBrowserSafari() ||
        
        // everyone else
        $this->checkBrowserNetPositive() ||
        $this->checkBrowserFirebird() ||
        $this->checkBrowserKonqueror() ||
        $this->checkBrowserIcab() ||
        $this->checkBrowserPhoenix() ||
        $this->checkBrowserAmaya() ||
        $this->checkBrowserLynx() ||
        $this->checkBrowserShiretoko() ||
        $this->checkBrowserIceCat() ||
        $this->checkBrowserW3CValidator() ||
        $this->checkBrowserMozilla() /* Mozilla is such an open standard that you must check it last */
    );
}
protected function checkBrowserOpera() {
    if( stripos($this->_agent,'opera mini') !== false ) {
        $resultant = stristr($this->_agent, 'opera mini');
        if( preg_match('/\//',$resultant) ) {
            $aresult = explode('/',$resultant);
            $aversion = explode(' ',$aresult[1]);
            $this->setVersion($aversion[0]);
        }
        else {
            $aversion = explode(' ',stristr($resultant,'opera mini'));
            $this->setVersion($aversion[1]);
        }
        $this->_browser_name = self::BROWSER_OPERA_MINI;
        $this->setMobile(true);
        return true;
    }
    else if( stripos($this->_agent,'opera') !== false ) {
        $resultant = stristr($this->_agent, 'opera');
        if( preg_match('/Version\/(10.*)$/',$resultant,$matches) ) {
            $this->setVersion($matches[1]);
        }
        else if( preg_match('/\//',$resultant) ) {
            $aresult = explode('/',str_replace("("," ",$resultant));
            $aversion = explode(' ',$aresult[1]);
            $this->setVersion($aversion[0]);
        }
        else {
            $aversion = explode(' ',stristr($resultant,'opera'));
            $this->setVersion(isset($aversion[1])?$aversion[1]:"");
        }
        $this->_browser_name = self::BROWSER_OPERA;
        return true;
    }
    return false;
}
protected function checkPlatform() {
    if( stripos($this->_agent, 'windows') !== false ) {
        $this->_platform = self::PLATFORM_WINDOWS;
    }
    else if( stripos($this->_agent, 'iPad') !== false ) {
        $this->_platform = self::PLATFORM_IPAD;
    }
    else if( stripos($this->_agent, 'iPod') !== false ) {
        $this->_platform = self::PLATFORM_IPOD;
    }
    else if( stripos($this->_agent, 'iPhone') !== false ) {
        $this->_platform = self::PLATFORM_IPHONE;
    }
    elseif( stripos($this->_agent, 'mac') !== false ) {
        $this->_platform = self::PLATFORM_APPLE;
    }
    elseif( stripos($this->_agent, 'android') !== false ) {
        $this->_platform = self::PLATFORM_ANDROID;
    }
    elseif( stripos($this->_agent, 'linux') !== false ) {
        $this->_platform = self::PLATFORM_LINUX;
    }
    else if( stripos($this->_agent, 'Nokia') !== false ) {
        $this->_platform = self::PLATFORM_NOKIA;
    }
    else if( stripos($this->_agent, 'BlackBerry') !== false ) {
        $this->_platform = self::PLATFORM_BLACKBERRY;
    }
    elseif( stripos($this->_agent,'FreeBSD') !== false ) {
        $this->_platform = self::PLATFORM_FREEBSD;
    }
    elseif( stripos($this->_agent,'OpenBSD') !== false ) {
        $this->_platform = self::PLATFORM_OPENBSD;
    }
    elseif( stripos($this->_agent,'NetBSD') !== false ) {
        $this->_platform = self::PLATFORM_NETBSD;
    }
    elseif( stripos($this->_agent, 'OpenSolaris') !== false ) {
        $this->_platform = self::PLATFORM_OPENSOLARIS;
    }
    elseif( stripos($this->_agent, 'SunOS') !== false ) {
        $this->_platform = self::PLATFORM_SUNOS;
    }
    elseif( stripos($this->_agent, 'OS\/2') !== false ) {
        $this->_platform = self::PLATFORM_OS2;
    }
    elseif( stripos($this->_agent, 'BeOS') !== false ) {
        $this->_platform = self::PLATFORM_BEOS;
    }
    elseif( stripos($this->_agent, 'win') !== false ) {
        $this->_platform = self::PLATFORM_WINDOWS;
    }
}



本方法的亮点在于可以判断手机和浏览器的电脑的浏览器及电脑手机的操作系统,如果你能把这几者结合起来用,基本,你在这个判断问题上就已经无敌了。。希望能帮到你。


php判断用户浏览器类型是否为微信浏览器

这个问题本站以前讨论过,直接点过去看吧:http://www.111com.net/phper/php/64226.htm

热门栏目