Previous Example-|-Next Chapter's Examples-|-Return to Chapter Listing

Example 4.7:
Special Characters in String Literals

We've created four variables:

twainA: This has quotation marks indicated like this--> \" and \'
twainB: This has the quotation marks, and we've entered a new line immediately before the word because, like this--> \n
twainC: This is the same as twainB, except that instead of using \n we used <BR>
twainD: This is the same as twainB, except that instead of using \n we used \t to place a Tab character.

The following shows what happens when you use the document.write instruction:

The following buttons show what happens when you place these strings into Alert boxes:



This is the script we used. First we declared these variables in the HEAD: <SCRIPT LANGUAGE="JAVASCRIPT"> <!-- var twainA = "\"I never write \'metropolis\' for seven cents, because I can get the same price for \'city.\' \" Mark Twain." var twainB = "\"I never write \'metropolis\' for seven cents, \nbecause I can get the same price for \'city.\' \" Mark Twain." var twainC = "\"I never write \'metropolis\' for seven cents, <BR>because I can get the same price for \'city.\' \" Mark Twain." var twainD = "\"I never write \'metropolis\' for seven cents, \tbecause I can get the same price for \'city.\' \" Mark Twain." //--> </SCRIPT> Then we wrote the variables to the Web page, like this: <SCRIPT LANGUAGE="JAVASCRIPT"> <!-- document.write("TwainA--quotation marks: <BR>" + twainA + "<P>") document.write("TwainB-- \n new line: <BR>" + twainB + "<P>") document.write("TwainC-- &lt;BR> line break: <BR>" + twainC + "<P>") document.write("TwainD-- \\t Tab: <BR>" + twainD + "<P>") //--> </SCRIPT> Then we created four buttons: <FORM> <input type="button" name="twain" value="twainA-- quotation marks" onclick="alert(twainA)"><P> <input type="button" name="twain" value="twainB-- \n new line" onclick="alert(twainB)"><P> <input type="button" name="twain" value="twainC-- <BR> line break" onclick="alert(twainC)"><P> <input type="button" name="twain" value=" twainD-- \t tab " onclick="alert(twainD)"> </FORM>
Previous Example-|-Next Chapter's Examples-|-Return to Chapter Listing