How to navigate to a URL while respecting Ctrl-click opens URL in new tab?
If you want to handle both clicks, normal and ctrl-click this is what I use :
$("li").on("click", function(e){
var url = $(this).find("a").attr("href");
if(e.ctrlKey){
$('<a href="'+ url + '" target="_blank"></a>')[0].click();
} else {
document.location = url;
}
return false;
});
This only works if you do it from a click handler triggered by the user, otherwise, the browser will detect it as an unwanted pop up and block it:
<div id="test">open in new tab</div>
$('#test').click(function(){
openInNewTab('http://example.com');
});
function openInNewTab(url)
{
$('<a href="'+ url + '" target="_blank">open in new tab</a>')[0].click();
}
I think there is no other option since this is a security behavior.