简体   繁体   English

使用 javascript 打开多个选项卡

[英]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.我有一个获取 ID 列表的 javascript 例程,我想为每个 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.首先,如果您实际上并不需要显示链接而只是用它来打开一个新标签页,您可以只使用window.open代替。

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 ). “技巧”是每次都提供不同的 URL 和一个新名称( window.open的第二个参数)。 Otherwise, Chrome (and presumably other browsers) will only open one tab.否则,Chrome(可能还有其他浏览器)只会打开一个标签页。

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:我找到了这个简单的 javascript 解决方案:

<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" Go 到 Chrome 设置>“隐私和安全”>“网站设置”>“弹出窗口和重定向”

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..."或者,在您尝试打开多个选项卡的选项卡上,您会看到一条消息“弹出窗口被阻止”,当您单击它时会给您一个对话框,您可以 select 单选按钮“始终允许弹出-向上和重定向从...”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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