简体   繁体   中英

How to open array of links correctly?

I have a question, how do I open all link from an array at the same time, not only one like it does now, I've tried something like this:

 var urls = ["https://www.google.com", "https://www.facebook.com"]; let main = function() { for (var i = 0; i < urls.length; i++) { window.open(urls[i], '_blank'); } }; main(); 

Can anybody show some examples? Thanks! :p

<html>
    <head>
        <script type='text/javascript'>
            var urls = ["https://www.google.com", "https://www.facebook.com"];
            function main() {
                for (var i = 0; i < urls.length; i++) {
                    var el = document.querySelector('#hidden') ;
                    //window.open(urls[i], '_blank');
                    var a = document.createElement('a');
                    a.href = urls[i];
                    a.target = '_blank';
                    el.appendChild(a);
                    a.click();
                }
            }
            window.addEventListener('load', function () { main();}, !1);
        </script>
    </head>
    <body>
        <div id='hidden'></div>
    </body>
</html>

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