Skip to content

How to retrieve a JS variable list from another file? #21560

Answered by Vic-ST
ghost asked this question in General
Discussion options

You must be logged in to vote

If I understood your question correctly then my answer is…

Here are three files test.js, script.js and index.html

test.js

var qrandom = Math.floor(Math.random() * (quotes.length));

function randomQuote()
{
document.getElementById('quote').innerHTML = quotes[qrandom];
}

randomQuote();

script.js

var quotes = [
    "Quote 1",
    "Quote 2"
]

and index.html

[blank file]

What you would do is add this code to the HTML page:

<script src="script.js"></script>
<script src="test.js"></script>

<div id="quote"></div>

Please tell me if I misunderstood your question.

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants