2015-01-09 62 views
1

我正在寻找以编程方式在保存操作的回调中打开新选项卡。 从http://jsfiddle.net/5Lwwmbq8/1/以编程方式在回调中打开新选项卡

var atag = document.createElement('a'); 
atag.setAttribute('href', 'https://yahoo.com'); 
atag.setAttribute('target', '_blank'); 
atag.setAttribute('id','clickdamnit'); 
$('#onlydiv').append(atag); 
setTimeout(function() { 
    atag.click(); 
}, 3000); 

差别不大。如果我不把它的回调之内的一切工作正常。但在回调的范围内,它会被弹出式窗口拦截器阻止。有没有解决的办法?

我试着用window.open代替锚标记 - 结果相同。

+5

浏览器阻止不是从用户操作启动的窗口的开放,所以推迟在暂停开放将无法工作,除了删除超时之外,没有解决方法。 – adeneo 2015-01-09 22:04:13

回答

1

我能够通过提供中间页面(例如'/ sameDomainDummySpinnerPage'),然后在回调中重定向来解决问题。

$('#linktest').click(function(){ 
    var childWindow = window.open('/sameDomainDummySpinnerPage'); 
    setTimeout(function() { 
     childWindow.location.replace('http://yahoo.com'); 
    }, 3000); 
}); 

另一种解决方法是将'target'属性设置为唯一的字符串。在相同目标的回调窗口中打开之前引入的选项卡。但是这会导致用户体验恶化。用户可以使用打开脚本的标签浏览网页。重新运行时,您的脚本将: 1.覆盖选项卡的现有window.location 2.可能不允许.focus();导致用户错过了他们的行动响应

裁判: https://developer.mozilla.org/en-US/docs/Web/API/Window.open#Do_not_use_target.3D.22_blank.22 http://www.infimum.dk/HTML/JSwindows.html Bypass popup blocker on window.open when JQuery event.preventDefault() is set

相关问题