How to Open Multiple Links Using a Single Anchor Tag

How can I open multiple links using a single anchor tag

You can certainly try this

Demo

<a href="http://www.microsoft.com" target="_blank" onclick="window.open('http://www.google.com'); window.open('http://www.yahoo.com');">Click Here</a>

How to open multiple links from a single hyperlink from anchor tag string in React?

Use regex to parse string and extract links, like this one:

const input = '<a href="https://www.google.com" target="_blank"> National ID </a><a href="https://www.yahoo.com" target="_blank"> National ID </a>';
const regex = /href="(\S*)"/g;
const links = Array.from(input.matchAll(regex), m => m[1]);

How to make a link open multiple pages when clicked

HTML:

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

JS:

$('a.yourlink').click(function(e) {
e.preventDefault();
window.open('http://yoururl1.com');
window.open('http://yoururl2.com');
});

window.open also can take additional parameters. See them here: http://www.javascript-coder.com/window-popup/javascript-window-open.phtml

You should also know that window.open is sometimes blocked by popup blockers and/or ad-filters.

Addition from Paul below: This approach also places a dependency on JavaScript being enabled. Not typically a good idea, but sometimes necessary.

Is it possible to put several URL in a href parameter?

The closest you could come would be to:

  1. Put a default URL in the href
  2. List alternative URLs elsewhere (such as in a data attribute)
  3. Bind a click event handler that runs some JavaScript which prevents the default behaviour and then tests each URL in turn until it finds one that works (or runs out).

Testing would require Ajax which would require either:

  • Permission via CORS from each URL you test
  • The user of a proxy (which would test availability of the URL to your proxy and not to the browser)

The additional HTTP requests would consume bandwidth (and thus time).

How can I open two pages from a single click without using JavaScript?

Without JavaScript, it's not possible to open two pages by clicking one link unless both pages are framed on the one page that opens from clicking the link. With JS it's trivial:

<p><a href="#" onclick="window.open('http://google.com');
window.open('http://yahoo.com');">Click to open Google and Yahoo</a></p>

Do note that this will be blocked by popup blockers built into web browsers but you are usually notified of this.



Related Topics



Leave a reply



Submit