John Coates
John Coates

Reputation: 476

make javascript links openable in a new tab

I have links that are present as <div> images with click events getting passed to javascript.

Javascript opens the links by using location.href = link;

When I middle click the links they don't open in a new tab though. How can i make them new tab compatible?

Upvotes: 2

Views: 601

Answers (3)

Amjad Masad
Amjad Masad

Reputation: 4035

you can use window.open(url) but it may considered as a popup

$('#divid').mousedown(function(e){if (e.which==2) window.open("url_here")});

e.which specifies which button was clicked 1 for left 2 for middle and 3 for right!

Upvotes: 1

Douglas
Douglas

Reputation: 37761

You should turn them into real hyperlinks:

<a href="page.html"><img alt="placeholder"></a>

Upvotes: 4

Ivo Wetzel
Ivo Wetzel

Reputation: 46745

Simply don't use JavaScript for links. location.href will change the url of the current document (window/tab).

Better set the href attribute per JavaScript, if you really need to use JS. Using href will also be a lot better for accessibility.

Upvotes: 1

Related Questions