1. 程式人生 > >JS 判斷當前訪問頁面的是手機、平板或電腦

JS 判斷當前訪問頁面的是手機、平板或電腦

var os = function () {
    var ua = navigator.userAgent,
    isWindowsPhone = /(?:Windows Phone)/.test(ua),
    isSymbian = /(?:SymbianOS)/.test(ua) || isWindowsPhone,
    isAndroid = /(?:Android)/.test(ua),
    isFireFox = /(?:Firefox)/.test(ua),
    isChrome = /(?:Chrome|CriOS)/.test(ua),
    isTablet = /(?:iPad|PlayBook)/.test(ua) || (isAndroid && !/(?:Mobile)/.test(ua)) || (isFireFox && /(?:Tablet)/.test(ua)),
    isPhone = /(?:iPhone)/.test(ua) && !isTablet,
    isPc = !isPhone && !isAndroid && !isSymbian;
    return {
        isTablet: isTablet,
        isPhone: isPhone,
        isAndroid: isAndroid,
        isPc: isPc
    };
}();
var printExcel = document.getElementById("PrintExcel");
var nurseExcel = document.getElementById("NurseExcel");
var loginDiv = document.getElementById("LoginDiv");
var theFooter = document.getElementById("TheFooter");


//alert(loginDiv);
/*a判斷是否是手機螢幕*/
if (os.isAndroid || os.isPhone) {
    if (printExcel != null) {
        printExcel.style.display = "none";
    }
    if (nurseExcel != null) {
        nurseExcel.style.display = "none";
    }
    if (loginDiv != null) {
        loginDiv.style.marginTop = "100px";
    }
    if (theFooter != null) {
        theFooter.style.display = "none";
    }
 


} else if (os.isTablet) {
    if (printExcel != null) {
        printExcel.style.display = "none";
    }
    if (nurseExcel != null) {
        nurseExcel.style.display = "none";
    }
    if (loginDiv != null) {


    }
    if (theFooter != null) {
        theFooter.style.display = "none";
    }
} else if (os.isPc) {
    if (nurseExcel != null) {
       // nurseExcel.style.display = "none";
    } if (loginDiv != null) {
        loginDiv.style.marginTop = "200px";
    }
    //alert("電腦端" + alarmAudioDiv);




}