以编程方式在回调中打开新选项卡

Programmatically opening a new tab within a callback

我希望在保存操作的回调中以编程方式打开一个新选项卡。 与 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 替换锚标记 - 结果相同。

我能够通过提供一个中间页面(比如“/sameDomainDummySpinnerPage”)然后在回调中重定向来解决问题。

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

我想到的另一种方法是将 'target' 属性设置为唯一字符串。在同一目标上的回调 window.open 会影响之前引入的选项卡。但这会导致用户体验恶化。用户可以使用选项卡、您的脚本打开来上网冲浪。在重新 运行 时,您的脚本将: 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