判断当前客户端是pc端还是移动端还是Ipad

实现手机和电脑两种不同的布局,判断手机还是电脑:window.onload一个页面只能有一个




	
    
    
	练习


    
1 2 3 4 5 6
1
2

判断当前客户端是pc端还是移动端还是Ipad

    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
      };
    }();
    if (os.isAndroid || os.isPhone) {
      console.log("手机");
    } else if (os.isTablet) {
      console.log("平板");
    } else if (os.isPc) {
      console.log("电脑");
    }

 

你可能感兴趣的:(前端)