js判断浏览器环境
const ua = navigator.userAgent.toLowerCase();
//微信小程序、微信公众号H5、浏览器、app环境
getENVIR: () => {
const isWeixin = ua.indexOf('micromessenger') !== -1;
const isInApp = /(^|;\s)app\//.test(ua);
if (isWeixin) {
if ((window as any).__wxjs_environment === 'miniprogram') {
return 'wxapp';
} else {
return 'wxh5';
}
} else {
if (!isInApp) {
return 'browser';
} else {
return 'app';
}
}
},
//终端判断:是否是IOS
checkIfIOS: () => {
return ua.match(/(iphone|ipod|ipad);?/i);
},
//终端判断:是否是Android
checkIfAndroid: () => {
return ua.match(/android|adr/i);
},
方式二:
testBrowser() {
let ua = window.navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == 'micromessenger') return 'weixin'; // 微信公众号
if (ua.match(/dsapp/i) == 'isapp') return 'other_app'; // 外部-app
//判断ua中是否含有和app端约定好的标识dsapp
// #ifdef APP-PLUS
return 'self_app'; // 自身-app
// #endif
// #ifdef MP-WEIXIN
return 'mp-weixin'; // 自身-小程序
// #endif
return 'h5';
},
方式三:
//函数
function isWx() {
var ua = navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == "micromessenger") {
return new Promise(resolve => {
wx.miniProgram.getEnv(function(res) {
if (res.miniprogram) {
resolve("mini-wx");
} else {
resolve("wx");
}
});
});
} else {
console.log("不在微信里");
return new Promise(resolve => {
resolve("no-wx");
});
}
}
//调用
isWx().then(type => {
if (type == "wx") {
console.log("这是微信环境中");
}
});
您还未登录, 登录 后可进行评论
发表
还没有评论哦,来抢个沙发吧!