random quotes with javascript

Soldato
Joined
2 Jun 2004
Posts
18,423
i'm trying to add a javascript random quotes block to my site. it's working fine with the following code...

Code:
var quotes = new Array;

quotes.push("Thank you!");

quotes.push("&quot;Here is another quote.&quot;<br><br>John Doe<br>ACME Inc.");

quotes.push("Keep adding quotes like this.");

document.write(quotes[(Math.floor(Math.random() * quotes.length))]);

the problem is that it stops working when i change the second quote to the following...

Code:
quotes.push("&quot;"blah blah blah blah blah blah blah blah blah blah...blah blah blah blah...blah blah...blah blah blah...?"&quot;<br><br>John Doe<br>ACME Inc.");
how can i fix it so that it works with that quote?
 
try this..

Code:
quotes.push('"Here is another quote."<br><br>John Doe<br>ACME Inc.');
or
Code:
quotes.push("\"Here is another quote.\"<br><br>John Doe<br>ACME Inc.");

option one used to be fine some years ago, perhaps it's changed to the latter..
Code:
[B]'[/B]   "blah"   [B]'[/B]
becomes
Code:
"   [B]\[/B]"blah[B]\[/B]"   "

EDIT: i can see the problem, "&quot;" and ...?"&quot
here's it fixed using \"
Code:
quotes.push("\"blah blah blah blah blah blah blah blah blah blah...blah blah blah blah...blah blah...blah blah blah...?\"<br><br>John Doe<br>ACME Inc.");
 
Last edited:
Yeah, the parser thinks that you're terminating the string with that double quote, which is pretty reasonable. You have to tell it that you want to actually display the quote character, which you do by escaping it with a backslash:

Code:
var foo = "Rob said \"I love beer\"";

which sets foo to:

Code:
Rob said "I love beer"

Characters you'll have to escape regularly will typically be ', " and \. The backslash needs to be escaped because it's the start of control characters, such as newlines and tabs. In the following example:

Code:
var foo = "This is a newline:\nI got the newline by entering \\n."

foo will be:

Code:
This is a newline:
I got the newline by entering \n.
 
looks like there is still a problem...

Code:
<script language="javascript" type="text/javascript">for (var i=0; i<quotes.length; document.write("<hr /><p>" + quotes[i++] + "</p>")) {};</script>
with this code i should be able to view all of my quotes on a single page. At the bottom of my random quotes block i want a "view all" link.

this works fine with the original quotes, but it doesn't work when i use hectors modification (above).

any ideas?
 
Last edited:
try to format the text and it'll make it easier for you to keep track of the code.

original:
Code:
for (var i=0; i<quotes.length; 
	 document.write("<hr /><p>" + quotes[i++] + "</p>")
) {};

corrected:
Code:
for (var i = 0; i<quotes.length; i++) {
	document.write("<hr /><p>"+quotes[i]+"</p>");
};

added i++ to the for statment.. moved a ) to close the for.. put the document.write into { } and ended it with a ; for good practice because multiline code would likely have issues..

i'm a bit to drunk to explain but take a few mins to look over it and have a read of this.
http://www.w3schools.com/js/js_loop_for.asp

edit: opps, i missed a bit.. also removed ++ from the quotes[i++] in the document.write
 
Last edited:
Back
Top Bottom