Insert a new line character into a string literal

W

Water Cooler v2

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?
 
M

Michael Winter

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.
 
W

Water Cooler v2

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?
 
R

RobG

Water said:
Thanks a tonne, Mike, for that invaluable advise. I am not sure I'd
have got those tips out of any tutorial.



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:



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>'+(document.documentElement||document.body).innerHTML.replace(/&/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
 
M

Michael Winter

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top