-2

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");
        }
    }
nonesuch
  • 167
  • 3
  • 16
  • 1
    `var data = {5, 8, 9};` and you have no errors in console? Also, would you be happy to come to a website that opens for you 1000 tabs or floating windows? No, of-course, therefore browsers will handle that for you - and give you a nice *no-no* blocked. – Roko C. Buljan Aug 03 '20 at 22:00
  • I was wondering the same thing re the data var. First I thought it would work as shorthand for `{5: 5, 8: 8, 9: 9}`, but I was wrong—the curlies in this case denote a *block*, with each comma-separated item executed separately as code. `data` should evaluate to 9 at the end. Either way, this presumably wasn't George's intention; I'm guessing he meant to use brackets instead. `[5, 8, 9]`. – Dennis Hackethal Aug 03 '20 at 22:07
  • Sorry, I meant brackets.\ – nonesuch Aug 03 '20 at 22:14

3 Answers3

4

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.

Dennis Hackethal
  • 13,662
  • 12
  • 66
  • 115
  • 1
    It's worth noting most modern browsers will block excessive usage of opening links (including from both .click() and window.open) – Ricky Hewitt Aug 03 '20 at 22:09
  • 3
    I tried this method and it still only opens the first tab, not any others. This is for an internal application and the users want multiple tabs, so please don't worry about whether 'this is the okay thing to do', I just want it to work. – nonesuch Aug 03 '20 at 22:22
  • Are you sure that both parameters change each time? And which browser are you using? – Dennis Hackethal Aug 03 '20 at 22:24
  • When I tried this, additional tabs did not open but because I have popup blocker enabled by default... once I enabled popups for this page, it opened 3 tabs without any problem. May be that's the issue with @George's browser too? – Alwaysa Learner Aug 04 '20 at 05:09
  • I'm using Chrome, and I don't have any popup blockers running. Each link is already different with a different key value, and I'm titling it with the variable counter. – nonesuch Aug 04 '20 at 14:36
0

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>
0

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..."