0

I am wondering if i can open http://example.com/some-page (clasic href link) in the same page that is viewed and the second link in a new tab.

<a href="http://example.com/some-page" second-url="http://example.com/other-page">link name</a>

I didn't found anything related to this on SO.

Is there a way to do it?

PS:

Please don't consider this a duplicate. The problem is that I will have multiple dynamic links (from DB) on a page. Can't add javascript for each link (this was the solution from other questions).

m3tsys
  • 3,939
  • 6
  • 31
  • 45

3 Answers3

2

This can be done with window.open(). You also need to call event.preventDefault() to prevent the default action of the a tag.

<a href="#">Click Here</a>

$('a').on("click", function(event) {
    event.preventDefault();
    window.open('http://www.facebook.com');
    window.open('http://www.espn.com');
});

Here is an example fiddle

http://jsfiddle.net/299tvrp9/

Richard Hamilton
  • 25,478
  • 10
  • 60
  • 87
  • Thanks but I will have multiple dynamic links (from DB) on a page. Can't add javascript for each link. – m3tsys Oct 20 '15 at 19:24
  • `Text` in your javascript `$(".multilink").click(function() { window.open($(this).attr("data-link1"), "_blank"); window.open($(this).attr("data-link2"), "_blank"); });` should do the trick (minus typos) – Puzzle84 Oct 20 '15 at 19:31
2

You could do a one-liner:

<a href="http://example.com/some-page" onclick="window.open('http://example.com/other-page')">link name</a>
j08691
  • 204,283
  • 31
  • 260
  • 272
  • Thank you, the code works but the problem is when i click it, it pushes me in a new tab ( onclick window.open link) and i need to remain on the same page and the second link to open in another tab. – m3tsys Oct 20 '15 at 19:31
  • @m3tsys - You can't change the browser's preference of what the browser focuses when opening a new tab. – j08691 Oct 20 '15 at 19:35
  • The problem is that i will need to keep `href` with the first link for seo purposes. – m3tsys Oct 20 '15 at 19:35
1

HTML

<a id="link-title">link name</a>

jQuery

$('#link-title').on('click', function(){
    window.open('https://linkaddress.com','_blank');
    window.open('https://linkaddresstwo.com','_blank');
});

Is that what you mean??

An0nC0d3r
  • 1,275
  • 13
  • 33