简体   繁体   中英

How can you access external JavaScript arrays in an html file?

I'm creating a website for a school project with different arrays of facts. I created multiple files with different JavaScript facts arrays and am trying to call them in the index.html file but I'm not sure how to call them without an a href tag. I've researched and most people say to try sourcing in the tag but nothing is printing.

This is my index.html code:

<!doctype html>
<html>
    <head>
        <meta charset="utf8" />
        <title></title>
        <link rel="stylesheet" type="text/css" href="main.css" />
        <script src="EllaFactsArray.html" type="text/javascript"></script>
    </head>
    <body>
        <p>
            Here is a fact about Ella:
        </p>
        <script>
            getEllaFact();
        </script>
    </body>
</html>

This is my facts array (EllaFactsArray.html):

<html>
<title>Ella Facts</title>
<head>
<script type="text/javascript">
    function getEllaFact()
    { 
        Ella = new Array(6);

        Ella[0] = "Ella had a six decade long career.";
        Ella[1] = "";
        Ella[2] = "";
        Ella[3] = "";
        Ella[4] = "";
        Ella[5] = "";

        i = Math.floor(Math.random() * Ella.length);

        document.write("<dt>" + Ella[i] + "\n");

    }
</script>
</head>
<body>
    <script type="text/javascript">
        getEllaFact();
    </script>
</body>
</html>

The script needs to reference a JavaScript file, not an HTML file containing a function. Move the function into its own file and include it in the pages as needed.

<script src="EllaFactsArray.js" type="text/javascript"></script>

https://www.w3schools.com/js/DEFAULT.asp

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