2

I have read the article.

I can click the "Open Windows" button to open multiple links in Chrome at once as new tabs with Code A.

At present, I hope to click the "Open Windows Of TextArea" button to open multiple links in textarea (which are http://www.msn.com and http://www.asp.net) in Chrome at once as new tabs, how can I do? Thanks!

Code A

<html>

<head>
    <script type="text/javascript">
        function open_win() {
            window.open("http://www.google.com/")
            window.open("http://www.twitter.com/")
        }

        function open_win_textArea() {
          //How to do ? 
        }


    </script>
</head>

<body>

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

    <br />


<textarea id="textID" name="message" rows="10" cols="80">
http://www.msn.com/          
http://www.asp.net/
</textarea>

    <br />
    <br />

    <form>
        <input type=button value="Open Windows Of TextArea" onclick="open_win_textArea()">
    </form>

</body>

</html>

Added Content:

To berriz: Thanks!

I have to use jQuery, the code B can work well.

Code B

<script type="text/javascript">       
    function open_win_textArea() {
        let links = $('#textID').val();

        // perform check for js not to break; you need to add defer as an attribute to the <script> element
        if (links) {
            let urls = links.split('\n');
            urls.forEach((url) => {
                window.open(url);
            });
        }
    }

</script>
HelloCW
  • 843
  • 22
  • 125
  • 310

1 Answers1

2

You first need to choose a delimiter. I see that you chose newline (\n).

Then, you can loop over each link in the textarea:

let links = document.getElementByID("textID").innerText;
// perform check for js not to break; you need to add defer as an attribute to the <script> element
if (links) {
    let urls = links.split('\n');
    urls.forEach((url) => {
        window.open(url);
    });
}

Hope this helps!

EDIT: Add the defer tag to the <script> element

berriz44
  • 62
  • 1
  • 1
  • 13