在项目中需要用到预览功能,一开始采用window.open(url)直接打开链接进入预览界面,但是在谷歌浏览器中不生效,网上查了一下相关问题,原因总结有两点:
- 在ajax请求的回调函数中window.open(url)是不生效的
(尝试在回调函数外面调用window.open(url),还是不行,坑) - window.open(url)在用户操作事件中才能生效,否则浏览器判定该操作不是用户操作,会阻止窗口弹出。
(这个比较靠谱)
但是预览的URL是通过ajax请求得到的,倒是可以增加一个预览按钮绑定点击事件,这个方法我没试过,我采用的是动态生成标签打开预览页的方法:
/*打开预览链接*/
openURL: function (url) {
let aLabel = document.createElement('a');
//设置链接
aLabel.setAttribute('href', url);
//新窗口打开链接
aLabel.setAttribute('target', '_blank');
//设置标签ID
aLabel.setAttribute('id', 'reportpoint');
// 防止反复添加
if (document.getElementById('reportpoint')) {
document.body.removeChild(document.getElementById('reportpoint'));
}
document.body.appendChild(aLabel);
aLabel.click();
console.log("打开链接:",url);
//window.open(url);
},