需求是页面外跳后,返回到项目首页,判断上一页有没有,有则后退一页,没有则关闭微信浏览器
处理方案:
创建refresh.js 文件 在index.html 文件中引入
home.vue
// 在首页先保存document.referrer 用于判断home.vue页面有无上一页var referrer = document.referrerlocalStorage.setItem('referrer' , referrer)
refresh.js文件
(function() {console.log(window.location.href)var href = window.location.href// 判断是否位首页if (href.indexOf('#/?') > -1) {// 页面加载是执行pageshowwindow.addEventListener('pageshow', function(event) {// event.persisted从缓存中获取时为true否则为false// window.performance.navigation.type == 2 网页通过“前进”或“后退”按钮加载,相当于常数// performance.navigation.TYPE_BACK_FORWARDif (event.persisted || window.performance && window.performance.navigation.type == 2) {var ua = navigator.userAgent// 判断设备为 iosif (!!ua.match(/\(i[^;]+;(U;)? CPU.+Mac OS X/)) {var res = JSON.parse(localStorage.getItem('res'))var limit_prd = res.limit_prd.split(',')// 判断页面是否为首页if (limit_prd.length == 1) {// document.referrer 上一页的urlvar referrer = localStorage.getItem('referrer ')if (window.history.length != 1 && referrer != '') {// 如果有上一页 就返回上一页window.history.go(-1)} else {// 无上一页 关闭微信浏览器if (ua.match(/MicroMessenger/i) == 'micromessenger') {WeixinJSBridge.call('closeWindow') //IOS //document.addEventListener('WeixinJSBridgeReady', function onBridgeReady() {//WeixinJSBridge.invoke("closeWindow", {}, function(e) {})//})// 安卓}}}}}})}})()