reptile7's JavaScript blog
Friday, April 01, 2005
More on Line-Breaks
Blog Entry #3

Let's briefly revisit the subject of line-breaks in JavaScript, which I discussed in my last blog entry. In doing some 'homework' for my up-and-coming discussion of HTMLGoodies' JavaScript Primers #2, I discovered some additional, and rather surprising (to me, at least), places where line-breaks can be put in a JavaScript command statement without interrupting the execution of the script. In illustration, consider the script in the 10-line 'document' below, which features in Joe Burns's discussion in Primer #2:

<script language="javascript">
document.write("text for the page")

I put the above code in a SimpleText file, which I named with an ".html" extension and executed on my hard disk directly (as opposed to FTPing it with Fetch to the server space that EarthLink gives me) with either Internet Explorer (5.1.6) or Netscape (7.02). With respect to the document.write("text for the page") command statement, I found that there are no fewer than five places where one can introduce a line-break without causing any problems, specifically:

(1) between document and .write("text for the page"), i.e., just before the period;

<script language="javascript">
.write("text for the page")

(2) between document. and write("text for the page"), i.e., just after the period;

<script language="javascript">
write("text for the page")

(3) between document.write and ("text for the page"), i.e., just before the left parenthesis (this mode of truncation appears in HTMLGoodies' JavaScript Primers #1, as you'll recall);

<script language="javascript">
("text for the page")

(4) between document.write( and "text for the page"), i.e., just after the left parenthesis;

<script language="javascript">
"text for the page")

and (5) just before the right parenthesis (notwithstanding Joe's representation to the contrary in Primer #2);

<script language="javascript">
document.write("text for the page"

Line-breaks at any other location in the document.write("text for the page") line will give an error.

Moreover, extra spaces via the space bar can be added at any of the acceptable-for-a-line-break locations above, for example:

<script language="javascript">
document          .write("text for the page")

(That's 10 spaces between document and .write("text for the page"), FYI.)

You can also put extra spaces between the words of the quoted code of the write( ) parameter:

<script language="javascript">
document.write("text          for the page")

but you can't put line-breaks here without getting an error, as we demonstrated last time.

So there you have it - everything you wanted to know about line-breaks but were afraid to ask, at least for the time being. Practical information or academic minutiae? You be the judge. I still think it's a good idea to be cautious about where you might put line-breaks in a script, however.


Comments: Post a Comment

<< Home

Powered by Blogger

Actually, reptile7's JavaScript blog is powered by Café La Llave. ;-)