How to make a link open multiple pages when clicked

JavascriptJqueryHtml

Javascript Problem Overview


I have two (or more) links. For example: http://google.com and http://yahoo.com.

How can I make them both open when I click on a single link?

For example, a link entitled "click here" which, when clicked, will open two different blank windows.

Javascript Solutions


Solution 1 - Javascript

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.

Solution 2 - Javascript

I did it in a simple way:

<a href="http://virtual-doctor.net" onclick="window.open('http://runningrss.com');
return true;">multiopen</a>

It'll open runningrss in a new window and virtual-doctor in same window.

Solution 3 - Javascript

You might want to arrange your HTML so that the user can still open all of the links even if JavaScript isn’t enabled. (We call this progressive enhancement.) If so, something like this might work well:

HTML
<ul class="yourlinks">
    <li><a href="http://www.google.com/"></li>
    <li><a href="http://www.yahoo.com/"></li>
</ul>
jQuery
$(function() { // On DOM content ready...
    var urls = [];
    
    $('.yourlinks a').each(function() {
        urls.push(this.href); // Store the URLs from the links...
    });

    var multilink = $('<a href="#">Click here</a>'); // Create a new link...
    multilink.click(function() {
        for (var i in urls) {
            window.open(urls[i]); // ...that opens each stored link in its own window when clicked...
        }
    });

    $('.yourlinks').replaceWith(multilink); // ...and replace the original HTML links with the new link.
});

This code assumes you’ll only want to use one “multilink” like this per page. (I’ve also not tested it, so it’s probably riddled with errors.)

Solution 4 - Javascript

You can open multiple windows on single click... Try this..

<a href="http://--" 
     onclick=" window.open('http://--','','width=700,height=700'); 
               window.open('http://--','','width=700,height=500'); ..// add more"
               >Click Here</a>`

Solution 5 - Javascript

You need to unblock the pop up windows for your browser and the code could work.

> chrome://settings/contentExceptions#popups

Chrome browser setting

Solution 6 - Javascript

I created a bit of a hybrid approach between Paul & Adam's approach:

The link that opens the array of links is already in the html. The jquery just creates the array of links and opens each one when the "open-all" button is clicked:

HTML:

<ul class="links">
<li><a href="http://www.google.com/"></a></li>
<li><a href="http://www.yahoo.com/"></a></li>
</ul>

<a id="open-all" href="#">OPEN ALL</a>

JQUERY:

$(function() { // On DOM content ready...
    var hrefs = [];

    $('.links a').each(function() {
        hrefs.push(this.href); // Store the URLs from the links...
    });
    
    $('#open-all').click(function() {
        for (var i in hrefs) {
            window.open(hrefs[i]); // ...that opens each stored link in its own window when clicked...
        }
    });
});

You can check it out here: https://jsfiddle.net/daveaseeman/vonob51n/1/

Solution 7 - Javascript

If you prefer to inform the visitor which links will be opened, you can use a JS function reading links from an html element. You can even let the visitor write/modify the links as seen below:

<script type="text/javascript"> 
    function open_all_links() {
        var x = document.getElementById('my_urls').value.split('\n');
        for (var i = 0; i < x.length; i++)
		    if (x[i].indexOf('.') > 0)
		    if (x[i].indexOf('://') < 0)
			    window.open('http://' + x[i]);
		    else
			    window.open(x[i]);
    }
</script>



<form method="post" name="input" action=""> 
    <textarea id="my_urls" rows="4" placeholder="enter links in each row..."></textarea>
    <input value="open all now" type="button" onclick="open_all_links();">
</form>

Solution 8 - Javascript

Here is a basic implementation in javascript - I separated it into an external file

HTML

<a href="" id="multi-link-opener" onclick="openMultipleLinks(myLinks)">Click To Open Links</a>

JS

var myLinks = ["https://google.com","https://www.w3schools.com/jsref/met_win_open.asp","https://developer.mozilla.org/en-US/docs/Web/API/Window/open"]

function openMultipleLinks(links) {
  for (var i = 0; i < links.length; i ++) {
    window.open(links[i]);
  }	
}

Note that the user will have to enable pop-ups for the pages to open.

Here it is in action: https://jsfiddle.net/cuppajoeman/rjavuhcg/

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionBenjaminView Question on Stackoverflow
Solution 1 - JavascriptAdam TerlsonView Answer on Stackoverflow
Solution 2 - JavascriptJeff_AlieffsonView Answer on Stackoverflow
Solution 3 - JavascriptPaul D. WaiteView Answer on Stackoverflow
Solution 4 - JavascriptIamat8View Answer on Stackoverflow
Solution 5 - JavascriptDu PengView Answer on Stackoverflow
Solution 6 - JavascriptdaveaseemanView Answer on Stackoverflow
Solution 7 - JavascriptBilal DadanlarView Answer on Stackoverflow
Solution 8 - JavascriptcuppajoemanView Answer on Stackoverflow