I need help loading a random caption each time my page loads. I have a text file with a string on each line, but I'm new to HTML and JavaScript.
Here's my code:
<div class="centerpiece">
<h1>DEL NORTE BANQUEST</h1>
<p class="caption"><script src = "js/caption.js"></script><script>getCaption();</script></p>
<a class="btn" id="browse-videos-button" href="#video-list">Browse Videos<br><img src="img/arrow-down.svg"style="width:15px;height:15px;"></a>
</div>
This is my JavaScript function:
function getCaption()
{
var txtFile = "text/captions.txt"
var file = new File(txtFile);
file.open("r");
var str = "";
var numLines = 0;
while (!file.eof)
{
numLines += 1;
}
file.close();
file.open("r");
var selectLine = Math.getRandomInt(0,numLines);
var currentLine = 0;
while(selectLine != currentLine)
{
currentLine += 1;
}
if(selectLine = currentLine)
{
str = file.readln();
}
file.close();
return str;
}
Text in Source File:
We talked yesterday
Freshman boys!
5/10
I'm having a heart attack *pounds chest super hard
The website is for my high school cross country team, just in case the text file confused you.
I'm still learning and not sure about all the syntax. I had some issues iterating through the file, so I opened and closed it twice. Here's a jsfiddle of what I'm trying to achieve:
https://jsfiddle.net/7cre9qqj/
If you need more code or have any critiques, please let me know. I appreciate any help and feedback as I continue to learn. Thank you!