Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

This is Your code :
myId.onclick = function(){`enter code here`
open('https://www.example1.com');
location.href = ('https://www.example2.com');
}

Change the code to:

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

Hope, you have got the solution for your problem.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading