zoukankan      html  css  js  c++  java
  • JS检测移动设备环境

    通过JS检测WeChat、Android、iOS环境

    代码块:

    // 检测微信状态下的手机设备环境
    function checkHJByWeChat() {
    	// 设备变量区分
    	let web = navigator.userAgent,
    		isiOS = !!web.match(/i[^;]+;(U;)? CPU.+Mac OS X/),
    		isAndroid = web.indexOf('Android') > -1 || web.indexOf('Adr') > -1,
    		isWeChat = web.toLocaleLowerCase().match(/MicroMessenger/i) == 'micromessenger';
    
    	let test = '';
    	// WeChat 环境下
    	if (isWeChat) {
    		console.log('当前环境为 WeChat');
    		if (isAndroid) { // Android 环境下
    			test = '当前环境为 WeChat--> Android';
    		} else if (isiOS) { // iOS 环境下 
    			test = '当前环境为 WeChat--> iOS';
    		} else {
    			test = '当前环境为 WeChat-->';
    		}
    	} else { // 非 WeChat 环境
    		console.log('当前环境为 非WeChat');
    		if (isAndroid) {
    			test = '当前环境为 非WeChat--> Android';
    		} else if (isiOS) {
    			test = '当前环境为 非WeChat--> iOS';
    		} else {
    			test = '当前环境为 非WeChat-->';
    		}
    	}
    	console.log(test);
    	document.querySelector('#test').innerHTML = test;
    }
    

    浏览器手机环境检测

    代码块:

    function checkHJ() {
    	let web = navigator.userAgent;
    	
    	let isWeChat = web.toLocaleLowerCase().match(/MicroMessenger/i) == 'micromessenger';
    	// WeChat
    	if (isWeChat) {
    		console.log('当前环境为 WeChat');
    	}
    
    	let isAndroid = web.indexOf('Android') > -1 || web.indexOf('Adr') > -1;
    	// Android
    	if (isAndroid) {
    		console.log('当前环境为 Android');
    	}
    	
    	let isiOS = !!web.match(/i[^;]+;(U;)? CPU.+Mac OS X/);
    	// iOS
    	if (isiOS) {
    		console.log('当前环境为 iOS');
    	}
    	
    	// others 浏览器
    	console.log('当前环境为 浏览器');
    }
    
  • 相关阅读:
    每天一个css text-indent
    每天一个css word-break word-wrap white-space
    [转载]CentOS+nginx+uwsgi+Python+django 环境搭建
    【转】django使用model创建数据库表使用的字段
    Django对mysql操作
    mysql增删改查
    mysql用户管理
    centos7启动mysql
    centos安装python3
    [转载]python学习目录(转至袁先生的博客)
  • 原文地址:https://www.cnblogs.com/zxk5211/p/14406095.html
Copyright © 2011-2022 走看看