项目场景:
微信端无法使用window.open进行文件预览
问题描述
在微信测试环境拿到要跳转的链接url,使用window.open(linkUrl),没有起作用,而且代码也没有报错
原因分析:
window.open会被浏览器拦截,导致无法正常弹出
解决方案:
首先,需要对是否是微信端进行判断,判断当前页面所在的环境是不是微信内置浏览器
const isWeiXin = function() {var ua = window.navigator.userAgent.toLowerCase();if (ua.match(/MicroMessenger/i) == 'micromessenger') {return true;} else {return false;}}
在微信浏览器环境下使用window.location.href
if (isWeiXin()){window.location.href = this.baseURL + res.data.data + '?token=' + localStorage.getItem('authorization');//验证token}else{window.open(this.baseURL + res.data.data, '_blank');}