jQuery: Javascript Link that opens in a new tab (target=_blank)

It’s actually a very small problem. When clicking on a certain DIV, I want a page to open in a new tab (!). Making the div a Link instead was not an option, so I searched for different approaches. window.open opens a new window in some browsers (as opposed to a new tab), and various hacks I found don’t work in all browsers, usually it’s Chrome that doesn’t comply.

The Solution: Simulate a form submit, the form having a blank target. Even Chrome is ok with this!

<div onclick="blankLink('http://www.ophidia.net');"> ... </div>

and in javascript the following happens:

function blankLink(page){
    $('body').append('<form id="blankform" action="'+page+'" target="_blank"></form>');
    $('#blankform').submit();
    $('#blankform').remove();
}

Works fine in all browsers so far.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.