Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Insert a new line character into a string literal (http://www.velocityreviews.com/forums/t924270-insert-a-new-line-character-into-a-string-literal.html)

Water Cooler v2 04-18-2006 09:03 AM

Insert a new line character into a string literal
 
Besides embedding <BR/>, like general purpose programming languages, I
thought you could embed escape sequences such as \n or \r\n into string
arguments to cause line breaks in JavaScript.


To test, I tried this:


<BODY>
<BR/><BR/>
<SCRIPT Language="JavaScript" type="text/javascript">
<!--
document.write("<B>Hello, World!</B>"+'\r\n'+"OK! In my next example,
I am going to put one JavaScript function in...")

document.write("<B>Hello, World!</B>"+"\r\nOK! In my next example, I am
going to put one JavaScript function in...")


document.write("<B>Hello, World!</B>"+"\n"+"OK! In my next example, I
am going to put one JavaScript function in...")

document.write("<B>Hello, World!</B>"+'"r\n"+"OK! In my next example, I
am going to put one JavaScript function in...")


//-->
</SCRIPT>
</BODY>


None of it inserts a line break on the display text.

A second question I have is, when does one use single quote delimited
strings and when double-quote delimited strings? Is it like C, wherein
single character values must be emebedded in single-quotes whereas
character array literals (or strings, if you will) must be packaged in
double-quoted strings? Or is it loosely defined?


Michael Winter 04-18-2006 09:30 AM

Re: Insert a new line character into a string literal
 
On 18/04/2006 10:03, Water Cooler v2 wrote:

> Besides embedding <BR/>,


You clearly aren't writing XHTML, so lose the XML empty-element syntax.

> like general purpose programming languages, I thought you could embed
> escape sequences such as \n or \r\n into string arguments to cause
> line breaks in JavaScript.


Yes, one can.

[snip]

> <SCRIPT Language="JavaScript" type="text/javascript">


The language attribute has long been deprecated. Omit it.

> <!--


'Hiding' script content has not been necessary for years. Omit markup
comments, too.

> document.write("<B>Hello, World!</B>"+'\r\n'+"OK! In my next example,
> I am going to put one JavaScript function in...")


[snip]

> None of it inserts a line break on the display text.


Of course not, for precisely the same reason that

<p>Some text split
over multiple lines.</p>

will render as one line (viewport width permitting): line terminators
are considered to be plain whitespace, just like tabs and spaces.

You should consider reading section "9.1 White space" in the HTML
specification.

When strings containing line terminators are written into a HTML
document, the line breaks will exist within the generated source.

> A second question I have is, when does one use single quote delimited
> strings and when double-quote delimited strings?


When it's more convenient to use one over the other.

> Is it like C, wherein single character values must be emebedded in
> single-quotes whereas character array literals (or strings, if you
> will) must be packaged in double-quoted strings? Or is it loosely
> defined?


The latter. One can pick and choose to best fit the circumstances.
Personally, I tend to use single quotes.

Mike


Please don't use tabs to indent code. Use a number of spaces, preferably
two, and four at most. Wrap lines manually to ensure that automatic
wrapping doesn't create syntax errors.

--
Michael Winter
Prefix subject with [News] before replying by e-mail.

Water Cooler v2 04-18-2006 10:11 AM

Re: Insert a new line character into a string literal
 
Thanks a tonne, Mike, for that invaluable advise. I am not sure I'd
have got those tips out of any tutorial.


>Of course not, for precisely the same reason that...


Thanks. I tried what you said and it worked. I did this:

<HTML>
<BODY>
<SCRIPT>
document.write("<PRE>foo\nbar</PRE>");
</SCRIPT>
</BODY>
</HTML>

and lo and behold! I got a line break interspercing the two words.


However, I could not completely understand the truth in this:

>When strings containing line terminators are written into a HTML
>document, the line breaks will exist within the generated source.



So, to try it, I removed the PRE tag in the above example and simply
made it:

document.write("foo\nbar");

and as expected, I got "foo bar" displayed on the browser, but the
source of the page was, just as I'd expected, the same as
document.write("foo\nbar"). I am sure I am misinterpreting that
statement of yours in someway. It'd be wiered if the client/browser
messed up with JavaScript and converted it to HTML source. What am I
missing?


RobG 04-18-2006 11:44 AM

Re: Insert a new line character into a string literal
 
Water Cooler v2 wrote:
> Thanks a tonne, Mike, for that invaluable advise. I am not sure I'd
> have got those tips out of any tutorial.
>
>
>> Of course not, for precisely the same reason that...

>
> Thanks. I tried what you said and it worked. I did this:
>
> <HTML>
> <BODY>
> <SCRIPT>
> document.write("<PRE>foo\nbar</PRE>");
> </SCRIPT>
> </BODY>
> </HTML>
>
> and lo and behold! I got a line break interspercing the two words.
>
>
> However, I could not completely understand the truth in this:
>
>> When strings containing line terminators are written into a HTML
>> document, the line breaks will exist within the generated source.

>
>
> So, to try it, I removed the PRE tag in the above example and simply
> made it:
>
> document.write("foo\nbar");
>
> and as expected, I got "foo bar" displayed on the browser, but the
> source of the page was, just as I'd expected, the same as
> document.write("foo\nbar").


Because that *is* the page source. What is displayed in the browser is
the generated code from the document.write statement. If you want to
see what the generated code looks like, create a bookmark with this as
the URL (remove newlines, paste as one single line with no spaces):

javascript:document.write('<code><ol><li>'+(docume nt.documentElement||document.body).innerHTML.repla ce(/&/g,"&amp;").replace(/</g,"&lt;").replace(/%20%20/g,"&nbsp;%20").replace(/\n/g,"<li>")+'<\/ol><\/code>');

Now you will see (with line numbers):

<script type="text/javascript">
document.write("foo\nbar");
</script>foo
bar


--
Rob

Michael Winter 04-18-2006 12:02 PM

Re: Insert a new line character into a string literal
 
On 18/04/2006 12:44, RobG wrote:

[snip]

> If you want to see what the generated code looks like, [...]


....download a recent version of Chris Pederick's Web Developer extension
for Firefox. :-)

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.


All times are GMT. The time now is 09:34 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.