回调函数中window.open()被拦截

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gui66497/article/details/75082318

在回调函数中window.open默认是会被拦截的,因为浏览器判断它不是用户自己打开的,存在安全风险,所以可以伪造一个用户点击事件来避开,代码如下:

function newWindow(url, id) { 
	var a = document.createElement('a'); 
	a.setAttribute('href', url); 
	a.setAttribute('target', '_blank');
	a.setAttribute('id', id); 
	if(!document.getElementById(id)) { 
	document.body.appendChild(a);
	} 
	a.click(); 
}
使用时直接将链接传入url就行,id可传可不传。

猜你喜欢

转载自blog.csdn.net/gui66497/article/details/75082318
今日推荐