因为有些浏览器会默认拦截window.open,当需要函数中打开窗口,可以使用a标签模拟window.open
/**
* a模拟window.open,不会被浏览器拦截
* @param {String} url a标签打开的地址
* @param {String} id a标签的ID
* @param {String} targetType a标签点击打开的方式(当前页面打开还是新窗口打开)
*/
openWindow: (url, targetType = '_blank', id = 'open', download = false) => {
// 如果存在则删除
if (document.getElementById(id)) {
document.body.removeChild(document.getElementById(id))
}
const a = document.createElement('a')
a.setAttribute('href', url)
if (download) {
a.setAttribute('download', url)
}
a.setAttribute('target', targetType)
a.setAttribute('id', id)
document.body.appendChild(a)
a.click()
}