简体   繁体   中英

Open multiple tabs with javascript

I have a javascript routine that gets a list of IDs and I want to open a browser tab for each ID. Here's what I have right now -- and it opens the first tab but not any other. Can someone point me in the right direction?

function launch() {
        var data = {5, 8, 9};
        if (data.length > 0) {

            let url = "/Controller/Action";
            data.forEach(function (entry) {
                
                let link = document.createElement('a');
                link.href = url + "?VisitID=" + entry;
                link.target = '_blank';
                link.click();
                //setTimeout(() => { console.log("wait"); }, 1000);
            });
        }
        else {
            alert("You must select at least one row before launching");
        }
    }

First of all, if you don't actually need to display the link and only use it to open a new tab, you can just use window.open instead.

Second, according to this comment , what you're trying to do can be done. Consider the following pseudo code:

[1,2,3].forEach(i => {
  window.open("http://example.com/" + i, i)
})

The "trick" is to provide both a different URL each time, and a new name (the second parameter to window.open ). Otherwise, Chrome (and presumably other browsers) will only open one tab.

But please , use this approach sparingly. If you open too many tabs too often it's going to drive users nuts.

I found this simple javascript solution:

<html>
<head>
<script type="text/javascript">
function open_win() {
    window.open("http://accountingtaxinsurance.com/")
    window.open("http://jaelfaulconinsurance.com/")
}
</script>
</head>

<body>
<form>
<input type=button value="Open Windows" onclick="open_win()">
</form>
</body>

</html>

Go to Chrome Settings > "Privacy and security" > "Site Settings" > "Pop-ups and redirects"

And either allow all sites, or add specific sites to the list of sites "Allowed to send pop-ups and use redirects"

Alternatively, on the tab from which you attempt to open multiple tabs, you will see a message "Pop up blocked", that when you click on it will give you a dialog, and you can select the radio-button "Always allow pop-ups and redirects from..."

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM