hind
hind

Reputation: 11

open link in new tab after click

I opened a new window by window.open in JavaScript, the probleme that it work just after refresh

    var url=document.getElementById("largeImage").getAttribute("href");

   $("#largeImage").click(function(e){ e.preventDefault();
   var win=window.open(url);



  });

Upvotes: 0

Views: 2449

Answers (2)

Locke Lamora
Locke Lamora

Reputation: 63

Why don't you just add an anchor around your image and open a new tab by using the href-attribute?

Example:

<a href="url"  target="_blank" >
<img />
</a>

Upvotes: 0

Alicia Sykes
Alicia Sykes

Reputation: 7117

Option 1, JavaScript approach

This will work

function openInNewTab(url) {
  var win = window.open(url, '_blank');
  win.focus();
}

You can then use it like this:

<div onclick="openInNewTab('www.test.com');">Something To Click On</div>

Reference, and more info, here: http://www.tutsplanet.com/open-url-new-tab-using-javascript-196/

Option 2, HTML approach

Really simple, just use the target='_blank' to your anchor link, e.g:

<a target="_blank" href="http://your_url_here.html">Link</a>

Upvotes: 1

Related Questions