将input_URL设置为一个数组,其中将包含多个URL

时间:2019-05-02 18:44:57

标签: javascript html arrays windows

在代码中,您可以看到一个名为input_URL的变量,在第44行的变量上附加了一个值,用户可以在其中输入网址,然后在112行中使用input_URL来打开新标签页。问题是我想在该变量中具有多个url,因为我希望用户能够存储多个URL并在需要时随时打开它们。我尝试使用数组来完成此操作,但我无法弄清楚,我到达了可以在同一数组中存储更多url的那部分,但不知道我将如何在第112行中访问它们(每个在特定时间) 如果您想尝试该脚本,我也添加了html脚本。

//variables
        var button1;
        var text;
        var button2;
        var buttonPlus;

        var breaks;

        var input_URL;



        var ids1 = 0;
        var input_value;
        var choose_title;
        //variables

        function addURL() 
        {

            ids1++;
            document.getElementById('firstButton').onclick='';

            text = document.createElement('SPAN');

            button1 = document.createElement('SPAN');

            breaks = document.createElement('BR');

            text.innerHTML='<input id="input" class="btn btn-primary" style="margin:2px" type="text" input type="reset" autocomplete="off" onclick="input.value = null " value="Enter URL" <br/>';



            button1.innerHTML='<input id="okButton" class="btn btn-primary" type="button" onclick="getInput()" value="OK" <br/>';

            document.body.append(text);
            document.body.append(button1);
            document.body.append(breaks);

        }

        function getInput()
        {
           choose_title = prompt("Choose a title for your URL");

++++++++LINE 44      input_URL = document.getElementById('input').value;



             if(choose_title == "")
                {
                    window.alert("PLEASE ENTER A TITLE");
                    getInput();
                }

            else if(choose_title == null){
                return;
            }

            else{



            //delete Elements
            text.parentNode.removeChild(text);
            button1.parentNode.removeChild(button1);

            var del3 = document.getElementById('firstButton');
            del3.parentNode.removeChild(del3);


            breaks.parentNode.removeChild(breaks);
            //delete Elements

            var del3 = document.getElementById('firstSpan');
            del3.parentNode.removeChild(del3)

            button2 = document.createElement('SPAN');
            document.body.append(button2); 
            button2.innerHTML='<input class="btn btn-primary" type="button" style="margin:2px" onclick="OpenInNewTab()" id="btn2">';



            ids++;

            document.getElementById('btn2').id = ids;

            document.getElementById(ids).setAttribute('value',choose_title);


            document.body.append(breaks);

            buttonPlus = document.createElement('SPAN');
            buttonPlus.id = 'firstSpan';
            buttonPlus.innerHTML = '<input id="firstButton" class="btn btn-primary" style="margin:2px" type="button" onclick="addURL()" value="+" <br/>';
            document.body.append(buttonPlus);

            }




            //Save input_value & title to computer


        }


        function OpenInNewTab()
        {
+++++++++LINE 112  link=window.open(input_URL, '_blank');
            //link.focus();
        }



<!DOCTYPE html>
<html lang="en">
<head>

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="stylesheet.css" integrity="sha384-      ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
    <title>Warper</title>
</head>

<body style="background-color:rgb(230, 255, 255)">

    <h1 class="display-1" style="color:aqua; font-size:90px; text-align:center; ">WARPER</h1>
    <p class="h6" style="text-align:center">Warp to anywhere you desire.</p>
    <span id="firstSpan">
    <input id="firstButton" style="margin:2px " class="btn btn-primary" type="button" onclick="addURL()"value="+">
    </span>


<script src="Script1.js"></script>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script>
</body>
</html>

0 个答案:

没有答案