Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make the <a> tag open two URLs (in two different pages)

I wanted to make the tag open two URLs at the same time. This is what I tried:

Only HTML

<a href="URL1" target="_blank" onclick="window.open('URL2');">text</a>

This did work but not the way I wanted it to. It would open the URL2 when clicked on it and if opened in a new tab with right click or the mouse wheel it would open URL1. I want it to open both pages in new tabs at the same time.

HTML + JavaScript HTML:

<a id="myId">text</a>

JS:

myId.onclick = function(){
open('https://www.example1.com');
location.href = ('https://www.example2.com');
}

This didn't work at all.

like image 634
Hilex23 Avatar asked Dec 02 '21 12:12

Hilex23


People also ask

Can a hyperlink open two pages?

No. Links by definition go to one destination. That is the way the web works; if they did what you ask, spammers would abuse it to the nth degree within weeks. It's part of the web protocol that each link go to one site and that you be able to see where it goes first.

How do I make one link open multiple tabs?

Just right click on the link and hold it to drag a box around the links. When you release the right click of your mouse, all those links will open in new tabs.

How do you put a 2 HREF in HTML?

You can try this in the anchor tag. Where in anchor tag use onclick method with the open method. Define both links using the open method, it will open two links with one click.

Can a tag have two href?

You can't have a link go to two places, but you can have it go to a php script that redirects to the second page when it's done doing whatever you need it to do.


Video Answer


1 Answers

This is Your code :

     myId.onclick = function(){
        open('https://www.example1.com');
        location.href = ('https://www.example2.com',,'_blank');
      }

Change the code to:

    myId.onclick = function(){ 
      window.open('https://www.example1.com','_blank');  //just use window.open() for both the cases;
      window.open('https://www.example2.com','_blank');
    }

Hope, you have got the solution for your problem.

like image 78
Arunkumar09 Avatar answered Oct 23 '22 02:10

Arunkumar09