473,480 Members | 1,857 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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?

Apr 18 '06 #1
4 58487
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.
Apr 18 '06 #2
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?

Apr 18 '06 #3
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
Apr 18 '06 #4
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.
Apr 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
38837
by: Dave Pylatuk | last post by:
Hello, I am getting the above error when executing a large insert statement against 8.1.7. This insert statement includes a very large string value, about 8000 characters long. Is there a limit...
16
17346
by: Don Starr | last post by:
When applied to a string literal, is the sizeof operator supposed to return the size of the string (including nul), or the size of a pointer? For example, assuming a char is 1 byte and a char *...
7
7059
by: herrcho | last post by:
i'm in the course of learning C, and found these two words "string, string literal" confusing me.. I'd like to know the difference between them.. Thank you
7
4340
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have...
4
5369
by: songkv | last post by:
Hi, I am trying to reassign an array of char to a string literal by calling a function. In the function I use pointer-to-pointer since I want to reassign the "string array pointer" to the string...
10
15878
by: Christopher Lusardi | last post by:
How would I put an end of line character in the second line below? Textbox1.Text = "Hello " Textbox1.Text = Textbox1.Text = "World" Thanks, Chris Lusardi
1
3580
by: iwongu | last post by:
Hi, I have a question about std::wcout and its underlying C functions. According to C99, a stream have an orientation type that is one of byte-oriented or wide-one, and it is determined by the...
18
605
by: william | last post by:
below is a short piece of code I wrote to testify my understanding of char *, and array. #include <stdio.h> int main() { char *str=NULL; char x="today is good!"; printf("%s", str);
8
2676
by: xmllmx | last post by:
It seems true, but I can't find any exact statement on this in the C or C++ standard. The C and C++ standard states: "__FILE_ The presumed name of the source file (a character string...
8
3044
by: mdh | last post by:
Hi all, I have a file, whose path is: "/Users/m/k&R/test_file" How do I include the '&' in a string constant? ( I need this for the example on p162). I have tried to use the Hex notation...
0
7055
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7010
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5362
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4499
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3003
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
203
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.