I'm trying to set up something that essentially chooses from a set of predefined phrases and displays it as a subheader on my page. What did I do wrong here?
<html>
<head>
<script type="text/javascript">
function RandomQuote()
{
var quotes= new Array();
quotes[0] = 'Home of Fall!'
quotes[1] = 'At Least This is Something!'
quotes[2] = 'Javascript!'
quotes[3] = 'Railroads at Last!'
quotes[4] = 'Better than Wikipedia'
quotes[5] = 'Now with Scorpions!'
quotes[6] = 'Irrelevant Stuff!'
quotes[7] = 'Fall is here!'
quotes[8] = 'That moment when Image-908329490283094.jpg'
quotes[9] = 'With Added Roundness!'
quotes[10] = 'Bacon!'
quotes[11] = 'Try Out WinterNstuff!'
quotes[12] = 'Try Out SpringNstuff!'
quotes[13] = 'Try Out SummerNstuff!'
quotes[14] = 'all html is lowercase!'
quotes[15] = '<div id=''youWishYouCouldReadThis''>InsertCodeHere</div>'
quotes[16] = 'In Remembrance of the soldiers who died in the Iranian War'
quotes[17] = 'Leaf Me Alone!'
quotes[18] = 'Pumpkins Were So 2011.'
quotes[19] = 'This Does not Count as a Random String'
quotes[20] = 'With Added Content Generation!'
return quotes[Math.floor((Math.random() * 19.9999))];
}
</script>
<title></title>
</head>
<body>
<div id="container">
<div id="header">
<table style="background-color:blue">
String:
<script type="text/javascript">document.write(RandomQuote());</script>
</table>
</body>
</div>
</div>
As a followup, is there a way to configure a title to do the same thing with random string generation?
You need to escape quotes in quotes[15]. Here is a working fiddle http://jsfiddle.net/ranjith19/HCCSW/
function RandomQuote()
{
var quotes= new Array();
quotes[0] = 'Home of Fall!'
quotes[1] = 'At Least This is Something!'
quotes[2] = 'Javascript!'
quotes[3] = 'Railroads at Last!'
quotes[4] = 'Better than Wikipedia'
quotes[5] = 'Now with Scorpions!'
quotes[6] = 'Irrelevant Stuff!'
quotes[7] = 'Fall is here!'
quotes[8] = 'That moment when Image-908329490283094.jpg'
quotes[9] = 'With Added Roundness!'
quotes[10] = 'Bacon!'
quotes[11] = 'Try Out WinterNstuff!'
quotes[12] = 'Try Out SpringNstuff!'
quotes[13] = 'Try Out SummerNstuff!'
quotes[14] = 'all html is lowercase!'
quotes[15] = '<div id=\'youWishYouCouldReadThis\'>InsertCodeHere</div>'
quotes[16] = 'In Remembrance of the soldiers who died in the Iranian War'
quotes[17] = 'Leaf Me Alone!'
quotes[18] = 'Pumpkins Were So 2011.'
quotes[19] = 'This Does not Count as a Random String'
quotes[20] = 'With Added Content Generation!'
return quotes[Math.floor((Math.random() * 19.9999))];
}
引用#15应该有正确的引用:
quotes[15] = '<div id="youWishYouCouldReadThis">InsertCodeHere</div>';
First, you are generating an index from 0..19.999 so quotes[20]
will never be used.
The best solution would be
return quotes[Math.random()*quotes.length];
Second, as mentioned by others, the quote #15 is not properly escaped:
quotes[15] = '<div id=''youWishYouCouldReadThis''>InsertCodeHere</div>'
should be
quotes[15] = '<div id=\'youWishYouCouldReadThis\'>InsertCodeHere</div>'
or
quotes[15] = '<div id="youWishYouCouldReadThis">InsertCodeHere</div>'
Third, there are easier ways to create a fixed array:
quotes = [
'Home of Fall!',
'At least This is Something!',
...
'With Added Content Generation'
]
Fourth, you shouldn't be placing a bare text inside the <table>
. A <p>
tag would be better in your case.
Fifth, your closing tags are not in the correct order. Credits go to @rlay3 for noticing this.
quotes[15] should have double quotes, or you need to escape the single quotes
quotes[15] = '<div id=\'youWishYouCouldReadThis\'>InsertCodeHere</div>'
or
quotes[15] = '<div id="youWishYouCouldReadThis">InsertCodeHere</div>'
PS your body/div closing tags are not in the correct order
Here is your code working.
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.