Tuesday, 20 August 2013

onchange open a page in a new tab

onchange open a page in a new tab

I am converting some <ul> into a <select> for small devices, like this:
$("#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'));
});
});
And i would like to open this links in a new tab, this is how i'm trying:
$("#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();
});
wich seems to do the job in Firefox but in chrome i'm getting the blocked
popup warning (it won't thought if the user clicks instead of simulating
it with JS
any workaround this?

No comments:

Post a Comment