473,545 Members | 2,782 Online
Bytes | Software Development & Data Engineering Community
+ 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="JavaS cript" 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 58494
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="JavaS cript" 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\n bar</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\n bar</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:docu ment.write('<co de><ol><li>'+(d ocument.documen tElement||docum ent.body).inner HTML.replace(/&/g,"&amp;").repl ace(/</g,"&lt;").repla ce(/%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
38854
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 on the size of statements that Oracle can execute ? Is this a setting that can be changed ? Thanks in advance.
16
17379
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 * is 4 bytes, should the following yield 4, 5, of something else? (And, if something else, what determines the result?) char x = "abcd"; printf(...
7
7063
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
4349
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 type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
4
5378
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 literal. But the second printf seems to give me garbage. Any advise on what I am doing wrong? Thanx
10
15890
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
3589
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 first using C function on that stream. And the specific orientation functions should not be applied to the stream that have other orientation.
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
2686
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 literal)." However, what the term "character string literal" means is not exactly defined. It can be interpreted as a string "C:\\HelloWorld.cpp" or a
8
3055
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 x26, as in "/Users/m/k\x26R/test_file".
1
7456
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7786
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6022
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5359
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5076
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3490
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1919
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 we have to send another system
1
1044
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.