使用Javascript检测桌面浏览器(非移动设备) [英] Detect Desktop Browser (not mobile) with Javascript

查看:76
本文介绍了使用Javascript检测桌面浏览器(非移动设备)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现以下代码可检测桌面浏览器.但是该方法还可以检测某些移动浏览器.如何仅检测Safari,IE,Firefox,Opera等桌面浏览器?

I found the following code to detect a desktop browser. But the method also detects some mobile browsers. How can I detect only desktop browsers like Safari, IE, Firefox, Opera etc?

is_desktopBrowser : function() {
    var ua = navigator.userAgent.toLowerCase();

    var rwebkit = /(webkit)[ \/]([\w.]+)/;
    var ropera = /(opera)(?:.*version)?[ \/]([\w.]+)/;
    var rmsie = /(msie) ([\w.]+)/;
    var rmozilla = /(mozilla)(?:.*? rv:([\w.]+))?/;

    var match = rwebkit.exec(ua) ||
            ropera.exec(ua) ||
            rmsie.exec(ua) ||
            ua.indexOf("compatible") < 0 && rmozilla.exec(ua) ||
            [];

    return { browser: match[1] || "", version: match[2] || "0" };
},

推荐答案

jQuery.browser在尝试找出哪个浏览器时会很有帮助. jQuery.browser在jQuery 1.9中已被删除.

jQuery.browser can be helpful when trying to figure out which browser. jQuery.browser was removed in jQuery 1.9.

我相信它基于navigator.UserAgent,但是如果您愿意的话,它可以基于navigator.UserAgent告诉您操作系统.

I believe it is based on navigator.UserAgent, however navigator.UserAgent can tell you the OS on its own if you want.

尝试一下:

var isMobile = navigator.userAgent.match(/(iPad)|(iPhone)|(iPod)|(android)|(webOS)/i)

这篇关于使用Javascript检测桌面浏览器(非移动设备)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆