This version of random text JS was written by Daniel Arce, after code by Don Sinclair. Updated by Nadine Wettlaufer.







Dealing with Quotation marks inside Quotation Marks in JavaScript

Notice how the content that gets written on the page is contained within quotation marks (") ?

When the content to be written on the page (both tags and text) is surrounded by quotation marks, you must not put any quotation marks inside the content (ie. as part of the content), or this will confuse the JavaScript.

For example, don't do this:
newContent = "I told her, "Because your friends don't dance, and if they don't dance then they ain't no friends of mine!"";
(look at those quotation marks inside quotation marks --yikes!)

However, there are ways to tell the browser which quotations JavaScript should ignore, considering them just part of the content:
One example is the JavaScript code on this page where t is 4 (and the following code will be written on to the page: <span class=\"bigorange\">PUMPKIN</span>) .

Notice how there is a backslash before the quotation mark right before the class name bigorange? And another backslash before the quotation mark right after bigorange? These backslashes tell the browser that the quotation marks that immediately follow them are not part of the JavaScript code that it needs to pay attention to. These quotation marks are to be considered just part of the content inside the other quotation marks.

So you could do this: newContent = "I told her, \"Because your friends don't dance, and if they don't dance then they ain't no friends of mine!\"";