How To Display Random Lines of Text From A TXT File: Get 3 Months Access To 400+ Books and Courses For Just $3/m!
How To Display Random Lines of Text From A TXT File: Get 3 Months Access To 400+ Books and Courses For Just $3/m!
Is there a way in javascript to display random lines of text (ie quotes) from a txt file? I know PHP
has this ability, but the server I’m working with does not support PHP.
Read online
force_flow2002:
Is there a way in javascript to display random lines of text (ie quotes) from a txt file?
In short: No, because Javascript doesn’t have the ability to read external files for security
reasons.
In long: It’s possible to refer to an external Javascript which defines the quotes. Here’s one using
some quotes used on a canoeing website I created some time ago: DEMO
<html>
<head>
<title>Random Quotes</title>
<script language="JavaScript" type="text/javascript" src="quoteArra
<script language="JavaScript" type="text/javascript">
<!--
function getQuote(){
https://www.sitepoint.com/community/t/how-to-display-random-lines-of-text-from-a-txt-file/1487/2 1/3
18/08/2020 How to display random lines of text from a txt file - JavaScript - The SitePoint Forums
var thisquote=Math.floor(Math.random()*(quotes.length));
document.write(quotes[thisquote]);
}
//-->
</script>
</head>
<body>
<script language="JavaScript" type="text/javascript">
<!--
getQuote();
//-->
</script>
</body>
</html>
Contents of quoteArray.js:
Andy
Thanks, Andy
https://www.sitepoint.com/community/t/how-to-display-random-lines-of-text-from-a-txt-file/1487/2 2/3