使用JavaScript脚本判断页面是否在微信中被打开


Posted in Javascript onMarch 06, 2016

1、根据User-Agent判断

// Js获取User-Agent 
window.navigator.userAgent 
 
if(window.navigator.userAgent.match(/MicroMessenger/i) == "micromessenger") 
 
// Php获取User-Agent 
echo $_SERVER['HTTP_USER_AGENT']; 
 
if(strpos($_SERVER['HTTP_USER_AGENT'],'MicroMessenger')==true) 
 
// 微信内部打开获取到的User-Agent 
Mozilla/5.0 (Linux; U; Android 5.0.2; zh-cn; MI 2S Build/LRX22G) AppleWebKit/533.1 (KHTML, like Gecko)Version/4.0 MQQBrowser/5.4 TBS/025488 Mobile Safari/533.1 MicroMessenger/6.3.9.48_refecd3e.700 NetType/WIFI Language/zh_CN

 
2、Url地址判断

// 你网页正常的url是 
https://forum.xxx.com/home.php?view=2015 
 
// 使用微信打开会在url后面添加几个参数 
https://forum.xxx.com/home.php?view=2015&from=timeline&isappinstalled=0

 
3、使用weixin-web-js判断是否是微信

! function(a, b) {
  "function" == typeof define && (define.amd || define.cmd) ? define(function() {
    return b(a)
  }) : b(a, !0)
}(this, function(a, b) {
  function c(b, c, d) {
    a.WeixinJSBridge ? WeixinJSBridge.invoke(b, e(c), function(a) {
      g(b, a, d)
    }) : j(b, d)
  }

  function d(b, c, d) {
    a.WeixinJSBridge ? WeixinJSBridge.on(b, function(a) {
      d && d.trigger && d.trigger(a), g(b, a, c)
    }) : d ? j(b, d) : j(b, c)
  }

  function e(a) {
    return a = a || {}, a.appId = E.appId, a.verifyAppId = E.appId, a.verifySignType = "sha1", a.verifyTimestamp = E.timestamp + "", a.verifyNonceStr = E.nonceStr, a.verifySignature = E.signature, a
  }

  function f(a) {
    return {
      timeStamp: a.timestamp + "",
      nonceStr: a.nonceStr,
      "package": a.package,
      paySign: a.paySign,
      signType: a.signType || "SHA1"
    }
  }

  function g(a, b, c) {
    var d, e, f;
    switch (delete b.err_code, delete b.err_desc, delete b.err_detail, d = b.errMsg, d || (d = b.err_msg, delete b.err_msg, d = h(a, d), b.errMsg = d), c = c || {}, c._complete && (c._complete(b), delete c._complete), d = b.errMsg || "", E.debug && !c.isInnerInvoke && alert(JSON.stringify(b)), e = d.indexOf(":"), f = d.substring(e + 1)) {
      case "ok":
        c.success && c.success(b);
        break;
      case "cancel":
        c.cancel && c.cancel(b);
        break;
      default:
        c.fail && c.fail(b)
    }
    c.complete && c.complete(b)
  }

  function h(a, b) {
    var e, f, c = a,
      d = p[c];
    return d && (c = d), e = "ok", b && (f = b.indexOf(":"), e = b.substring(f + 1), "confirm" == e && (e = "ok"), "failed" == e && (e = "fail"), -1 != e.indexOf("failed_") && (e = e.substring(7)), -1 != e.indexOf("fail_") && (e = e.substring(5)), e = e.replace(/_/g, " "), e = e.toLowerCase(), ("access denied" == e || "no permission to execute" == e) && (e = "permission denied"), "config" == c && "function not exist" == e && (e = "ok"), "" == e && (e = "fail")), b = c + ":" + e
  }

  function i(a) {
    var b, c, d, e;
    if (a) {
      for (b = 0, c = a.length; c > b; ++b) d = a[b], e = o[d], e && (a[b] = e);
      return a
    }
  }

  function j(a, b) {
    if (!(!E.debug || b && b.isInnerInvoke)) {
      var c = p[a];
      c && (a = c), b && b._complete && delete b._complete, console.log('"' + a + '",', b || "")
    }
  }

  function k() {
    0 != D.preVerifyState && (u || v || E.debug || "6.0.2" > z || D.systemType < 0 || A || (A = !0, D.appId = E.appId, D.initTime = C.initEndTime - C.initStartTime, D.preVerifyTime = C.preVerifyEndTime - C.preVerifyStartTime, H.getNetworkType({
      isInnerInvoke: !0,
      success: function(a) {
        var b, c;
        D.networkType = a.networkType, b = "http://open.weixin.qq.com/sdk/report?v=" + D.version + "&o=" + D.preVerifyState + "&s=" + D.systemType + "&c=" + D.clientVersion + "&a=" + D.appId + "&n=" + D.networkType + "&i=" + D.initTime + "&p=" + D.preVerifyTime + "&u=" + D.url, c = new Image, c.src = b
      }
    })))
  }

  function l() {
    return (new Date).getTime()
  }

  function m(b) {
    w && (a.WeixinJSBridge ? b() : q.addEventListener && q.addEventListener("WeixinJSBridgeReady", b, !1))
  }

  function n() {
    H.invoke || (H.invoke = function(b, c, d) {
      a.WeixinJSBridge && WeixinJSBridge.invoke(b, e(c), d)
    }, H.on = function(b, c) {
      a.WeixinJSBridge && WeixinJSBridge.on(b, c)
    })
  }
  var o, p, q, r, s, t, u, v, w, x, y, z, A, B, C, D, E, F, G, H;
  if (!a.jWeixin) return o = {
    config: "preVerifyJSAPI",
    onMenuShareTimeline: "menu:share:timeline",
    onMenuShareAppMessage: "menu:share:appmessage",
    onMenuShareQQ: "menu:share:qq",
    onMenuShareWeibo: "menu:share:weiboApp",
    onMenuShareQZone: "menu:share:QZone",
    previewImage: "imagePreview",
    getLocation: "geoLocation",
    openProductSpecificView: "openProductViewWithPid",
    addCard: "batchAddCard",
    openCard: "batchViewCard",
    chooseWXPay: "getBrandWCPayRequest"
  }, p = function() {
    var b, a = {};
    for (b in o) a[o[b]] = b;
    return a
  }(), q = a.document, r = q.title, s = navigator.userAgent.toLowerCase(), t = navigator.platform.toLowerCase(), u = !(!t.match("mac") && !t.match("win")), v = -1 != s.indexOf("wxdebugger"), w = -1 != s.indexOf("micromessenger"), x = -1 != s.indexOf("android"), y = -1 != s.indexOf("iphone") || -1 != s.indexOf("ipad"), z = function() {
    var a = s.match(/micromessenger\/(\d+\.\d+\.\d+)/) || s.match(/micromessenger\/(\d+\.\d+)/);
    return a ? a[1] : ""
  }(), A = !1, B = !1, C = {
    initStartTime: l(),
    initEndTime: 0,
    preVerifyStartTime: 0,
    preVerifyEndTime: 0
  }, D = {
    version: 1,
    appId: "",
    initTime: 0,
    preVerifyTime: 0,
    networkType: "",
    preVerifyState: 1,
    systemType: y ? 1 : x ? 2 : -1,
    clientVersion: z,
    url: encodeURIComponent(location.href)
  }, E = {}, F = {
    _completes: []
  }, G = {
    state: 0,
    data: {}
  }, m(function() {
    C.initEndTime = l()
  }), H = {
    config: function(a) {
      E = a, j("config", a);
      var b = E.check === !1 ? !1 : !0;
      m(function() {
        var a, d, e;
        if (b) c(o.config, {
          verifyJsApiList: i(E.jsApiList)
        }, function() {
          F._complete = function(a) {
            C.preVerifyEndTime = l(), G.state = 1, G.data = a
          }, F.success = function() {
            D.preVerifyState = 0
          }, F.fail = function(a) {
            F._fail ? F._fail(a) : G.state = -1
          };
          var a = F._completes;
          return a.push(function() {
            k()
          }), F.complete = function() {
            for (var c = 0, d = a.length; d > c; ++c) a[c]();
            F._completes = []
          }, F
        }()), C.preVerifyStartTime = l();
        else {
          for (G.state = 1, a = F._completes, d = 0, e = a.length; e > d; ++d) a[d]();
          F._completes = []
        }
      }), E.beta && n()
    },
    ready: function(a) {
      0 != G.state ? a() : (F._completes.push(a), !w && E.debug && a())
    },
    error: function(a) {
      "6.0.2" > z || B || (B = !0, -1 == G.state ? a(G.data) : F._fail = a)
    },
    checkJsApi: function(a) {
      var b = function(a) {
        var c, d, b = a.checkResult;
        for (c in b) d = p[c], d && (b[d] = b[c], delete b[c]);
        return a
      };
      c("checkJsApi", {
        jsApiList: i(a.jsApiList)
      }, function() {
        return a._complete = function(a) {
          if (x) {
            var c = a.checkResult;
            c && (a.checkResult = JSON.parse(c))
          }
          a = b(a)
        }, a
      }())
    },
    onMenuShareTimeline: function(a) {
      d(o.onMenuShareTimeline, {
        complete: function() {
          c("shareTimeline", {
            title: a.title || r,
            desc: a.title || r,
            img_url: a.imgUrl || "",
            link: a.link || location.href,
            type: a.type || "link",
            data_url: a.dataUrl || ""
          }, a)
        }
      }, a)
    },
    onMenuShareAppMessage: function(a) {
      d(o.onMenuShareAppMessage, {
        complete: function() {
          c("sendAppMessage", {
            title: a.title || r,
            desc: a.desc || "",
            link: a.link || location.href,
            img_url: a.imgUrl || "",
            type: a.type || "link",
            data_url: a.dataUrl || ""
          }, a)
        }
      }, a)
    },
    onMenuShareQQ: function(a) {
      d(o.onMenuShareQQ, {
        complete: function() {
          c("shareQQ", {
            title: a.title || r,
            desc: a.desc || "",
            img_url: a.imgUrl || "",
            link: a.link || location.href
          }, a)
        }
      }, a)
    },
    onMenuShareWeibo: function(a) {
      d(o.onMenuShareWeibo, {
        complete: function() {
          c("shareWeiboApp", {
            title: a.title || r,
            desc: a.desc || "",
            img_url: a.imgUrl || "",
            link: a.link || location.href
          }, a)
        }
      }, a)
    },
    onMenuShareQZone: function(a) {
      d(o.onMenuShareQZone, {
        complete: function() {
          c("shareQZone", {
            title: a.title || r,
            desc: a.desc || "",
            img_url: a.imgUrl || "",
            link: a.link || location.href
          }, a)
        }
      }, a)
    },
    startRecord: function(a) {
      c("startRecord", {}, a)
    },
    stopRecord: function(a) {
      c("stopRecord", {}, a)
    },
    onVoiceRecordEnd: function(a) {
      d("onVoiceRecordEnd", a)
    },
    playVoice: function(a) {
      c("playVoice", {
        localId: a.localId
      }, a)
    },
    pauseVoice: function(a) {
      c("pauseVoice", {
        localId: a.localId
      }, a)
    },
    stopVoice: function(a) {
      c("stopVoice", {
        localId: a.localId
      }, a)
    },
    onVoicePlayEnd: function(a) {
      d("onVoicePlayEnd", a)
    },
    uploadVoice: function(a) {
      c("uploadVoice", {
        localId: a.localId,
        isShowProgressTips: 0 == a.isShowProgressTips ? 0 : 1
      }, a)
    },
    downloadVoice: function(a) {
      c("downloadVoice", {
        serverId: a.serverId,
        isShowProgressTips: 0 == a.isShowProgressTips ? 0 : 1
      }, a)
    },
    translateVoice: function(a) {
      c("translateVoice", {
        localId: a.localId,
        isShowProgressTips: 0 == a.isShowProgressTips ? 0 : 1
      }, a)
    },
    chooseImage: function(a) {
      c("chooseImage", {
        scene: "1|2",
        count: a.count || 9,
        sizeType: a.sizeType || ["original", "compressed"],
        sourceType: a.sourceType || ["album", "camera"]
      }, function() {
        return a._complete = function(a) {
          if (x) {
            var b = a.localIds;
            b && (a.localIds = JSON.parse(b))
          }
        }, a
      }())
    },
    previewImage: function(a) {
      c(o.previewImage, {
        current: a.current,
        urls: a.urls
      }, a)
    },
    uploadImage: function(a) {
      c("uploadImage", {
        localId: a.localId,
        isShowProgressTips: 0 == a.isShowProgressTips ? 0 : 1
      }, a)
    },
    downloadImage: function(a) {
      c("downloadImage", {
        serverId: a.serverId,
        isShowProgressTips: 0 == a.isShowProgressTips ? 0 : 1
      }, a)
    },
    getNetworkType: function(a) {
      var b = function(a) {
        var c, d, e, b = a.errMsg;
        if (a.errMsg = "getNetworkType:ok", c = a.subtype, delete a.subtype, c) a.networkType = c;
        else switch (d = b.indexOf(":"), e = b.substring(d + 1)) {
          case "wifi":
          case "edge":
          case "wwan":
            a.networkType = e;
            break;
          default:
            a.errMsg = "getNetworkType:fail"
        }
        return a
      };
      c("getNetworkType", {}, function() {
        return a._complete = function(a) {
          a = b(a)
        }, a
      }())
    },
    openLocation: function(a) {
      c("openLocation", {
        latitude: a.latitude,
        longitude: a.longitude,
        name: a.name || "",
        address: a.address || "",
        scale: a.scale || 28,
        infoUrl: a.infoUrl || ""
      }, a)
    },
    getLocation: function(a) {
      a = a || {}, c(o.getLocation, {
        type: a.type || "wgs84"
      }, function() {
        return a._complete = function(a) {
          delete a.type
        }, a
      }())
    },
    hideOptionMenu: function(a) {
      c("hideOptionMenu", {}, a)
    },
    showOptionMenu: function(a) {
      c("showOptionMenu", {}, a)
    },
    closeWindow: function(a) {
      a = a || {}, c("closeWindow", {}, a)
    },
    hideMenuItems: function(a) {
      c("hideMenuItems", {
        menuList: a.menuList
      }, a)
    },
    showMenuItems: function(a) {
      c("showMenuItems", {
        menuList: a.menuList
      }, a)
    },
    hideAllNonBaseMenuItem: function(a) {
      c("hideAllNonBaseMenuItem", {}, a)
    },
    showAllNonBaseMenuItem: function(a) {
      c("showAllNonBaseMenuItem", {}, a)
    },
    scanQRCode: function(a) {
      a = a || {}, c("scanQRCode", {
        needResult: a.needResult || 0,
        scanType: a.scanType || ["qrCode", "barCode"]
      }, function() {
        return a._complete = function(a) {
          var b, c;
          y && (b = a.resultStr, b && (c = JSON.parse(b), a.resultStr = c && c.scan_code && c.scan_code.scan_result))
        }, a
      }())
    },
    openProductSpecificView: function(a) {
      c(o.openProductSpecificView, {
        pid: a.productId,
        view_type: a.viewType || 0,
        ext_info: a.extInfo
      }, a)
    },
    addCard: function(a) {
      var e, f, g, h, b = a.cardList,
        d = [];
      for (e = 0, f = b.length; f > e; ++e) g = b[e], h = {
        card_id: g.cardId,
        card_ext: g.cardExt
      }, d.push(h);
      c(o.addCard, {
        card_list: d
      }, function() {
        return a._complete = function(a) {
          var c, d, e, b = a.card_list;
          if (b) {
            for (b = JSON.parse(b), c = 0, d = b.length; d > c; ++c) e = b[c], e.cardId = e.card_id, e.cardExt = e.card_ext, e.isSuccess = e.is_succ ? !0 : !1, delete e.card_id, delete e.card_ext, delete e.is_succ;
            a.cardList = b, delete a.card_list
          }
        }, a
      }())
    },
    chooseCard: function(a) {
      c("chooseCard", {
        app_id: E.appId,
        location_id: a.shopId || "",
        sign_type: a.signType || "SHA1",
        card_id: a.cardId || "",
        card_type: a.cardType || "",
        card_sign: a.cardSign,
        time_stamp: a.timestamp + "",
        nonce_str: a.nonceStr
      }, function() {
        return a._complete = function(a) {
          a.cardList = a.choose_card_info, delete a.choose_card_info
        }, a
      }())
    },
    openCard: function(a) {
      var e, f, g, h, b = a.cardList,
        d = [];
      for (e = 0, f = b.length; f > e; ++e) g = b[e], h = {
        card_id: g.cardId,
        code: g.code
      }, d.push(h);
      c(o.openCard, {
        card_list: d
      }, a)
    },
    chooseWXPay: function(a) {
      c(o.chooseWXPay, f(a), a)
    }
  }, b && (a.wx = a.jWeixin = H), H
});
Javascript 相关文章推荐
window.showModalDialog使用手册
Jan 11 Javascript
在js中使用&quot;with&quot;语句中跨frame的变量引用问题
Mar 08 Javascript
Js 随机数产生6位数字
May 13 Javascript
JavaScript中的parse()方法使用简介
Jun 12 Javascript
详解javascript函数的参数
Nov 10 Javascript
使用jQuery的ajax方法向服务器发出get和post请求的方法
Jan 13 Javascript
js实现首屏延迟加载实现方法 js实现多屏单张图片延迟加载效果
Jul 17 Javascript
在vue中使用setInterval的方法示例
Apr 16 Javascript
JavaScript 斐波那契数列 倒序输出 输出100以内的质数代码实例
Sep 11 Javascript
Node.js实现简单管理系统
Sep 23 Javascript
javascript操作向表格中动态加载数据
Aug 27 Javascript
使用node-media-server搭建一个简易的流媒体服务器
Jan 20 Javascript
JavaScript实现图片滑动切换的代码示例分享
Mar 06 #Javascript
使用jQuery或者原生js实现鼠标滚动加载页面新数据
Mar 06 #Javascript
AngularJS页面访问时出现页面闪烁问题的解决
Mar 06 #Javascript
JavaScript模拟数组合并concat
Mar 06 #Javascript
JavaScript模拟push
Mar 06 #Javascript
JavaScript中利用jQuery绑定事件的几种方式小结
Mar 06 #Javascript
Node.js模块封装及使用方法
Mar 06 #Javascript
You might like
《星际争霸》各版本雷兽特点图文解析 雷兽不同形态一览
2020/03/02 星际争霸
用PHP与XML联手进行网站编程代码实例
2008/07/10 PHP
php把数据表导出为Excel表的最简单、最快的方法(不用插件)
2014/05/10 PHP
WordPress中查询文章的循环Loop结构及用法分析
2015/12/17 PHP
动态表单验证的操作方法和TP框架里面的ajax表单验证
2017/07/19 PHP
Laravel模型间关系设置分表的方法示例
2018/04/21 PHP
PHP正则表达式处理函数(PCRE 函数)实例小结
2019/05/09 PHP
使一个函数作为另外一个函数的参数来运行的javascript代码
2007/08/13 Javascript
jQuery操作表格(table)的常用方法、技巧汇总
2014/04/12 Javascript
jQuery图片切换插件jquery.cycle.js使用示例
2014/06/16 Javascript
ECMAScript6函数剩余参数(Rest Parameters)
2015/06/12 Javascript
jQuery拖拽排序插件制作拖拽排序效果(附源码下载)
2016/02/23 Javascript
Node.js 应用跑得更快 10 个技巧
2016/04/03 Javascript
JS获取随机数和时间转换的简单实例
2016/07/10 Javascript
微信小程序 网络请求(GET请求)详解
2016/11/16 Javascript
jQuery+ajax实现动态添加表格tr td功能示例
2018/04/23 jQuery
axios的拦截请求与响应方法
2018/08/11 Javascript
CKEditor4配置与开发详细中文说明文档
2018/10/08 Javascript
js中对象和面向对象与Json介绍
2019/01/21 Javascript
深入剖析JavaScript instanceof 运算符
2019/06/14 Javascript
Javascript数组及类数组相关原理详解
2020/10/29 Javascript
对numpy 数组和矩阵的乘法的进一步理解
2018/04/04 Python
python Selenium实现付费音乐批量下载的实现方法
2019/01/24 Python
详解python中list的使用
2019/03/15 Python
Django集成CAS单点登录的方法示例
2019/06/10 Python
Python中断多重循环的几种方式详解
2020/02/10 Python
Python如何存储数据到json文件
2020/03/09 Python
在django中使用post方法时,需要增加csrftoken的例子
2020/03/13 Python
Python  word实现读取及导出代码解析
2020/07/09 Python
python爬取微博评论的实例讲解
2021/01/15 Python
傲盾软件面试题
2015/08/17 面试题
仓库主管的岗位职责
2013/12/04 职场文书
护士年终考核评语
2014/12/31 职场文书
60条职场经典语录,总有一条能触动你的心
2019/08/21 职场文书
MySQL实例精讲单行函数以及字符数学日期流程控制
2021/10/15 MySQL
海贼王十大逆天果实 魂魂果实上榜,岩浆果实攻击力最强
2022/03/18 日漫