2013-08-20 90 views
0

我把一些<ul><select>爲小型設備,如:在新標籤中打開一個頁面,JS

$("#blog aside .widget, #blog-single aside .widget").each(function() { 
      var widget = $(this); 
      $("<select />").appendTo(widget); 
      /* option en blanco*/ 
      $("<option />", { 
       "value" : '', 
       "text" : widget.find('h3').text()+'..' 
      }).appendTo(widget.find('select')); 
      widget.find('ul li a').each(function(){ 
       var el = $(this); 
       $("<option />", { 
        "value" : el.attr("href"), 
        "text" : el.text() 
       }).appendTo(widget.find('select')); 
      }); 
     }); 

,我想在新標籤中打開此鏈接,這是怎麼了我試圖:

$("#blog select, #blog-single select").change(function() { 
      var url = $(this).find("option:selected").val(); 
       /* simulamos target _blank, ya que son externos */ 
       var a = document.createElement('a'); 
       a.href= url; 
       a.className = 'external'; 
       a.target = '_blank'; 
       document.body.appendChild(a);    
       a.click(); 
     }); 

至極,似乎做的工作在Firefox,但在鉻,我發現了阻止彈出警告(如果用戶點擊與JS

模擬它,而不是它不會想到

任何解決方法呢?

+0

[在新標籤使用javascript打開URL](http://stackoverflow.com/questions/4907843/open-url-in-new-tab-using-javascript) – jcubic

回答

0

這就是我們如何結束了「解決」了,

檢查,如果彈出exsist如果沒有,則退回到當前標籤:

var url = $(this).val(); 
var win = window.open(url); 
/* Detectar posible bloqueo de popups */ 
if (win == null || typeof(win) == "undefined" || (win == null && win.outerWidth == 0) || (win != null && win.outerHeight == 0) || win.test == "undefined"){ 
        window.location.href = url; 
}else if (win){ 
    win.onload = function(){ 
    if (win.screenX === 0) { 
      win.close(); 
    } 
    }; 
} 
0

爲了避免您可以使用窗體調用屏幕

$("#blog select, #blog-single select").change(function() { 
    var url = $(this).find("option:selected").val(); 
    /* simulamos target _blank, ya que son externos */ 
    var form = document.createElement('form'); 
    form.action= url; 
    form.method = 'get'; 
    form.target = '_blank'; 
    document.body.appendChild(form);    
    form.submit(); 
    setTimeout(function(){ 

     document.body.removeChild(form); 

    },1000); 


}); 
+0

您好的可能重複,謝謝你的時間。這給了我相同的結果,儘管... –

+0

對不起,但瀏覽器阻止未經用戶交互打開的新窗口,嘗試添加事件與window.open裏面。 http://stackoverflow.com/questions/12247368/javascript-window-open-function-opens-link-without-popup-blocker –

相關問題