Home > Web Front-end > JS Tutorial > body text

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

PHP中文网
Release: 2016-05-16 15:02:56
Original
2035 people have browsed it

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

<script type="text/javascript">
//手机端判断各个平台浏览器及操作系统平台
function checkPlatform(){
    if(/android/i.test(navigator.userAgent)){
        document.write("This is Android&#39;browser.");//这是Android平台下浏览器
    }
    if(/(iPhoneiPadiPodiOS)/i.test(navigator.userAgent)){
        document.write("This is iOS&#39;browser.");//这是iOS平台下浏览器
    }
    if(/Linux/i.test(navigator.userAgent)){
        document.write("This is Linux&#39;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&#39;browser.");//这是微信平台下浏览器
    }
}
 $(document).ready(function(){
  alert(navigator.platform);
  checkPlatform();
 });
</script>
Copy after login

注意上面的代码使用了jquery。需要加载jquery才可以运行。当然你可以把触发函数给删除了

在这里就不做pc端操作系统平台的判断了,怎么判断也说下方法,如用document.write(navigator.platform);就可以获得操作系统平台。在win32下就会显示Win32,;在Win64下就会显示Win64等。

<script type="text/javascript">
var browser = {
    versions : function () {
        var u = navigator.userAgent,
        app = navigator.appVersion;
        return {
            trident : u.indexOf(&#39;Trident&#39;) > -1,
            presto : u.indexOf(&#39;Presto&#39;) > -1,
            webKit : u.indexOf(&#39;AppleWebKit&#39;) > -1,
            gecko : u.indexOf(&#39;Gecko&#39;) > -1 && u.indexOf(&#39;KHTML&#39;) == -1,
            mobile : !!u.match(/AppleWebKit.*Mobile.*/) || !!u.match(/AppleWebKit/),
            ios : !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/),
            android : u.indexOf(&#39;Android&#39;) > -1 || u.indexOf(&#39;Linux&#39;) > -1,
            iPhone : u.indexOf(&#39;iPhone&#39;) > -1 || u.indexOf(&#39;Mac&#39;) > -1,
            iPad : u.indexOf(&#39;iPad&#39;) > -1,
            webApp : u.indexOf(&#39;Safari&#39;) == -1,
            QQbrw : u.indexOf(&#39;MQQBrowser&#39;) > -1,
            weiXin : u.indexOf(&#39;MicroMessenger&#39;) > -1,
            ucLowEnd : u.indexOf(&#39;UCWEB7.&#39;) > -1,
            ucSpecial : u.indexOf(&#39;rv:1.2.3.4&#39;) > -1,
            ucweb : function () {
                try {
   return parseFloat(u.match(/ucweb\d+\.\d+/gi).toString().match(/\d+\.\d+/).toString()) >= 8.2
                } catch (e) {
                    if (u.indexOf(&#39;UC&#39;) > -1) {
                        return true;
                    } else {
                        return false;
                    }
                }
            }
            (),
            Symbian : u.indexOf(&#39;Symbian&#39;) > -1,
            ucSB : u.indexOf(&#39;Firefox/1.&#39;) > -1
        };
    }
    ()
}
if (browser.versions.QQbrw){
document.write("qq浏览器");
}else
{
document.write("其它浏览器");
}
</script>
Copy after login

   


以上就是js判断手机浏览器操作系统和微信浏览器的方法_javascript技巧的内容,更多相关内容请关注PHP中文网(www.php.cn)!


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!