学问简说
window.open 敕令在用户操纵(trusted events)时, 才会一般的翻开应当页面而不会被浏览器阻拦
ps : 什么是 trusted events
trusted events is evnet.isTrusted is true. The isTrusted read-only property of the Event interface is a boolean that is true when the event was generated by a user action, and false when the event was created or modified by a script or dispatched via dispatchEvent.
论述题目
许多时刻,我们是须要在做异步要求返来时才跳转,这时候,假如守候到要求返回再去window.open 会被浏览器阻拦。
处理方案
场景1,假如是在发送要求后守候要求返来,不论要求效果准确与否都要跳转页面的话
须要在发送要求前先var A = window.open() 一个新窗口。然后等要求完毕了,在给这个窗口附上URL
function clickAction() {
var openWind = window.open()
ajaxAgent('POST', 'URL', params).then((resutl)=> {
opneWind.loaction.href = 'jumpURL'
})
}
- '注重' 这里会有个题目,假如异步要求的时候太长,会让用户觉得有个空缺窗口在那里晾着O__O …。
场景2,假如是想发送要求后守候要求返来,经由过程要求效果来决议是不是要跳转,这时候,就没发经由过程实行异步要求之前先翻开个新窗口的方法来处理了,由于有可能会放回构造是失利,不愿望跳转,这时候,在去吧空窗口封闭,会给用户带来疑心。
现在就不能防止被阻拦了,只能关照用户,让用户去给被阻拦的页面新增加信托。
var openNewWindow = window.open(url);
// 推断跳转页面是不是被阻拦了。
// 假如已阻拦,就给他用户提醒
// 提醒的体式格局可所以指导用户去处理被阻拦题目(增加信托)
if (!openNewWindow || openNewWindow.closed || typeof openNewWindow.closed === 'undefined') {
alert('您的付出界面窗口已被浏览器阻拦')
}