简体   繁体   English

React.js:如何连续多次运行 window.open()?

[英]React.js: How to run window.open() multiple times in succession?

I use a few state variables to determine which sites should be opened in new tabs (or maybe a new window if tabs aren't possible) with a single button click.我使用几个 state 变量来确定哪些站点应该在新选项卡中打开(或者如果选项卡不可用,则可能是一个新的 window),只需单击一下按钮。 However, window.open() only opens the first link.但是, window.open() 仅打开第一个链接。

In this code I tried pushing the target sites to an array and running.forEach and.map on the array items.在此代码中,我尝试将目标站点推送到数组并在数组项上运行.forEach 和.map。

open_selected_websites() {
    const sites_to_open = [];

    // check each property for true and array.push if so
    this.final_social_media_site_selections.facebook && sites_to_open.push('http://facebook.com');
    this.final_social_media_site_selections.twitter && sites_to_open.push('http://twitter.com');
    this.final_social_media_site_selections.linkedin && sites_to_open.push('http://linkedin.com');
    this.final_social_media_site_selections.instagram && sites_to_open.push('http://instagram.com');
    this.final_social_media_site_selections.pinterest && sites_to_open.push('http://pinterest.com');

    console.log(sites_to_open); // all observables are true and all sites appear in the array.

    sites_to_open.forEach((social_media_site) => {
        // setTimeout(() => {
            window.open(social_media_site);
        // }, 500)
    })

In both cases, facebook loaded in a new tab.在这两种情况下,facebook 都会加载到新选项卡中。 It is the first array item.它是第一个数组项。

Then I tried adding a setTimeout to see if some time space might affect things.然后我尝试添加一个 setTimeout 以查看某些时间空间是否会影响事物。 No, still only Facebook.不,仍然只有 Facebook。

Then I tried testing only one site in each function:然后我尝试在每个 function 中只测试一个站点:

<Button
    size='huge'
    color='orange'
    onClick={ () => {
        // final_edits_store.open_selected_websites();
        final_edits_store.test_to_open_twitter();
        final_edits_store.test_to_open_facebook();

    } }
>
    Copy Text and Open Social Media Sites in New Tabs
</Button>

In this case Twitter opened.在这种情况下 Twitter 打开。 The twitter function was listed first. twitter function 排在首位。

Does anyone know what is causing window.open() not to fire multiple times?有谁知道是什么导致 window.open() 不能多次触发? And how to overcome this limit?以及如何克服这个限制?

You can't. 你不能

Browsers only allow a single window to be triggered from a given user interaction. 浏览器仅允许从给定的用户交互触发单个窗口。

This is a security feature to prevent websites bombing the user with vast numbers of new windows. 这是一项安全功能,可防止网站使用大量新窗口轰炸用户。

jsFiddle here - https://jsfiddle.net/523bLxf4/12/ 这里的jsfiddle - https://jsfiddle.net/523bLxf4/12/

Try the name parameter that window.open takes. 尝试使用window.open需要的name参数。 I was able to open multiple windows. 我能够打开多个窗口。

Instead of window.open(social_media_site); 代替window.open(social_media_site); try window.open(social_media_site, social_media_site); 尝试window.open(social_media_site, social_media_site);

In the name parameter use some tag that uniquely identifies the window. name参数中,使用一些唯一标识窗口的标签。

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

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