判断浏览器(微信、微博、QQ)、操作系统、横竖屏等

一、根据浏览器判断URL跳转

var browserOfMobile = {
    wapUrl: 'http://m.baidu.com',
    versions:function(){
        var u = navigator.userAgent;
        return {
            //mobile: !!u.match(/AppleWebKit.*Mobile.*/)||!!u.match(/AppleWebKit/), //是否为移动终端
            mobile: u.match(/(iPhone|iPod|Android|ios|iPad)/i),
        };
    }()
}
if (browserOfMobile.versions.mobile && browserOfMobile.wapUrl != '') {
    location.href = browserOfMobile.wapUrl;
}

二、低版本IE判断

(function() {
    if(navigator.userAgent.indexOf('MSIE') != -1) {
        var ieBrowser = navigator.userAgent.split(';')[1];
        if(ieBrowser.indexOf('MSIE') != -1 && parseInt(($.trim(ieBrowser)).substr(5)) < 9.0) {
            window.location.href = 'browser_download.html';
        }
    }
}());

三、判断横屏还是竖屏

function orient() {
    if (window.orientation == 0 || window.orientation == 180) {
        $("body").attr("class", "portrait");
        orientation = 'portrait';
        return false;
    }
    else if (window.orientation == 90 || window.orientation == -90) {
        $("body").attr("class", "landscape");
        orientation = 'landscape';

        return false;
    }
}

四、监测浏览器是android的还是ios的

var browser = {
    versions: function() {
        var u = navigator.userAgent;
        return {//移动终端浏览器版本信息 
            trident: u.indexOf('Trident') > -1, //IE内核
            presto: u.indexOf('Presto') > -1, //opera内核
            webKit: u.indexOf('AppleWebKit') > -1, //苹果、谷歌内核
            gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐内核
            mobile: !!u.match(/AppleWebKit.*Mobile.*/) || !!u.match(/AppleWebKit/), //是否为移动终端
            ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios终端
            android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android终端或者uc浏览器
            iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否为iPhone或者QQHD浏览器
            iPad: u.indexOf('iPad') > -1, //是否iPad
            webApp: u.indexOf('Safari') == -1 //是否web应该程序,没有头部与底部
        };
    }(),
    language: (navigator.browserLanguage || navigator.language).toLowerCase()
}

if (browser.versions.ios || browser.versions.iPhone || browser.versions.iPad) {
    window.location="https://itunes.apple.com/xxx";
}
else if (browser.versions.android) {
    window.location="http://xxx/xxx.apk";
}

// document.writeln("语言版本: " + browser.language);
// document.writeln(" 是否为移动终端: " + browser.versions.mobile);
// document.writeln(" ios终端: " + browser.versions.ios);
// document.writeln(" android终端: " + browser.versions.android);
// document.writeln(" 是否为iPhone: " + browser.versions.iPhone);
// document.writeln(" 是否iPad: " + browser.versions.iPad);
// document.writeln(navigator.userAgent);

五、判断手机类型

window.onload = function () {
    var u = navigator.userAgent;
    if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) { //安卓手机
        alert("安卓手机");
    } else if (u.indexOf('iPhone') > -1) { //苹果手机
        alert("苹果手机");
    } else if (u.indexOf('Windows Phone') > -1) { //winphone手机
        alert("winphone手机");
    }
}

六、判断操作系统

function detectOS() {
    var sUserAgent = navigator.userAgent;
    var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
    var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh") || (navigator.platform == "MacIntel");
    if (isMac) return "Mac";
    var isUnix = (navigator.platform == "X11") && !isWin && !isMac;
    if (isUnix) return "Unix";
    var isLinux = (String(navigator.platform).indexOf("Linux") > -1);
    if (isLinux) return "Linux";
    if (isWin) {
        var isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1 || sUserAgent.indexOf("Windows 2000") > -1;
        if (isWin2K) return "Win2000";
        var isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1 || sUserAgent.indexOf("Windows XP") > -1;
        if (isWinXP) return "WinXP";
        var isWin2003 = sUserAgent.indexOf("Windows NT 5.2") > -1 || sUserAgent.indexOf("Windows 2003") > -1;
        if (isWin2003) return "Win2003";
        var isWinVista= sUserAgent.indexOf("Windows NT 6.0") > -1 || sUserAgent.indexOf("Windows Vista") > -1;
        if (isWinVista) return "WinVista";
        var isWin7 = sUserAgent.indexOf("Windows NT 6.1") > -1 || sUserAgent.indexOf("Windows 7") > -1;
        if (isWin7) return "Win7";
    }
    return "other";
}
document.writeln("您的操作系统是:" + detectOS());

七、判断APP浏览器(微信、微博、QQ、android、ios)

var ua = navigator.userAgent.toLowerCase();//获取判断用的对象
if (ua.match(/MicroMessenger/i) == "micromessenger") {
    //在微信中打开
}
if (ua.match(/WeiBo/i) == "weibo") {
    //在新浪微博客户端打开
}
if (ua.match(/QQ/i) == "qq") {
    //在QQ空间打开
}
if (browser.versions.ios) {
    //是否在IOS浏览器打开
}
if(browser.versions.android){
    //是否在安卓浏览器打开
}

你可能感兴趣的:(JavaScript,前端开发)