判断当前是否是微信浏览器

var ua = navigator.userAgent.toLowerCase();
var isWeixin = ua.indexOf('micromessenger') != -1;
console.log(isWeixin);

微信浏览器

微信公众号或微信内直接打开链接

var ua = navigator.userAgent.toLowerCase();
if (ua.match(/MicroMessenger/i) == "micromessenger") {
  // 在微信中打开
}

微信小程序

// html 引入
<script type="text/javascript" src="https://res.wx.qq.com/open/js/jweixin-1.3.2.js"></script>
// js中如下判断
var ua = navigator.userAgent.toLowerCase();
if(ua.match(/MicroMessenger/i)=="micromessenger") {
  wx.miniProgram.getEnv((res)=>{
    if (res.miniprogram) {
      // 在微信小程序中打开
    }
  })
}

支付宝浏览器

支付宝浏览器中

var ua = navigator.userAgent.toLowerCase();
if (ua.match(/Alipay/i)=="alipay") {
  // 在支付宝中打开
}

支付宝小程序中

// html 引入
<script type="text/javascript" src="https://appx/web-view.min.js"></script>
// js中如下判断
var ua = navigator.userAgent.toLowerCase();
if (ua.match(/Alipay/i)=="alipay") {
  my.getEnv((res)=>{
    if (res.miniprogram) {
      // 在支付宝小程序中打开
    }
  })
}

app壳子

在app壳子中往往使用往ua里面添加唯一标识符来做标识。

var ua = navigator.userAgent.toLowerCase();
if (ua.match(/myapp/i)=="myapp") {
  // 在自家app壳子里面
}

其他的没有标识的app,最后的倔强

可以使用路径上新增参数的方式去识别,当识别到参数就在session保存起来,这样作用周期就是某一次打开,防止了数据污染。