2015-01-09 67 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

相關問題