2013-08-20 93 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 –

相关问题