how to create a MultiLine string

Î

κÖлª

I want to create a multiline string that can works in ie and
firefox,
who have a better idea ?
 
E

Evertjan.

=?gb2312?B?zrrW0Luq?= wrote on 24 aug 2007 in comp.lang.javascript:
I want to create a multiline string that can works in ie and
firefox,

Good for you.
who have a better idea ?

Go to the beach?

============================

Please state your real question. Is this it?

"Can I write a javascript stringvariable containing more lines?"

Yes you can.

"Could you give me an example?"

<script type='text/javascript'>

var myString = 'Hello<br>World.<br><br>This is a test.';
document.write(myString);

var myOtherString = 'Hello\nWorld.\n\nThis is another test.';
alert(myOtherString);

</script>
 
G

Guest

I want to define a multiline string.

like:

var html = "<html>
<body>
</body>
</html>
.....
"
but i don't want to use '\'
 
E

Evertjan.

hong hua wrote on 24 aug 2007 in comp.lang.javascript:
I want to define a multiline string.

Are you responding on a previous posting? [Re: ..]

Please always quote on usenet!
like:

var html = "<html>
<body>
</body>
</html>
....
"

Why do you want to do that, Hua?
but i don't want to use '\'

Why don't you want to do that?

How could you use '\' for that?
 
G

Guest

if i want to store the following html code in a variable(javascript of
course) ,
Do you have a better idea?

================== HTML Start ======================
I want to define a multiline string.

Are you responding on a previous posting? [Re: ..]

Please always quote on usenet!
var html = "<html>
<body>
</body>
</html>
....
"
<script language="javascript">
alert("hello world");
</script>
================= HTML End =======================
 
P

Peter Michaux

var html = "<html>" +
"<body>" +
"<\/body>" +
"<\/html>";

Why escape the '/' on the closing tags? I have only read that is
necessary for script tags. Do HTML parsers exist that have a problem
with those closing tags or are you just being conservative?

I find it a easier to use single quotes when quoting HTML because then
attribute double quotes don't need escaping.

"<li id=\"foo\">"
'<li id="foo">'

Peter
 
T

Thomas 'PointedEars' Lahn

Douglas said:
I want to define a multiline string.
[...]
but i don't want to use '\'

var html = "<html>" +
"<body>" +
"<\/body>" +
"<\/html>";

I would always prefer

var html = new Array(
"<html>",
" <body>"
" <\/body>",
"<\/html>"
).join("\n");

above that.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Peter said:
Why escape the '/' on the closing tags? I have only read that is
necessary for script tags.

Your information is inaccurate. CDATA content ends at an ETAGO delimiter,
no matter the element.

http://www.w3.org/TR/html4/types.html#type-cdata
Do HTML parsers exist that have a problem with those closing tags

Yes, the W3C Validator's SGML parser being one of them.
or are you just being conservative?

Maybe that as well. It is not necessary to escape ETAGOs if they do not
occur within `script' element CDATA content. So the said snippet has the
best chance to work everywhere.
I find it a easier to use single quotes when quoting HTML because then
attribute double quotes don't need escaping.

"<li id=\"foo\">"
'<li id="foo">'

Add me. However, it is perfectly Valid to use apostrophes ("single quotes")
as attribute delimiter as well, even in X(HT)ML.

(Unfortunately, the SGML spec is not available free of charge at ISO.)
http://www.w3.org/TR/html4/intro/sgmltut.html#h-3.2.2
http://www.w3.org/TR/REC-xml/#NT-AttValue


PointedEars
 
S

Steve Swift

Douglas said:
var html = "<html>" +
"<body>" +
"<\/body>" +
"<\/html>";

Aha! If we can create a variable with a multiline string (using the OP's
nice description) then I wonder if I can use it to set a TITLE
attribute, and if the resulting tooltip in Opera/Firefox will be multiline?

This will stretch/extend my meagre JavaScript skills.
 
T

Thomas 'PointedEars' Lahn

Steve said:
Aha! If we can create a variable with a multiline string (using the OP's
nice description)

...., which would be one including "\n", "\r\n" or "\r", ...
then I wonder if I can use it to set a TITLE attribute, and if the
resulting tooltip in Opera/Firefox will be multiline?

No. Opera ignores "\n", and Firefox includes a space for it. Both include
a space for "\r". Tested in Opera 9.23.8808 and Firefox 2.0.0.6 on Windows
XP Service Pack 2.

Note that the HTML 4.01 Specification does not say the `title' attribute
value has to be displayed as a tooltip in a conforming UA.

http://www.w3.org/TR/REC-html40/struct/global.html#adef-title

If you need a tooltip, use a styled element.


PointedEars
 
T

Thomas 'PointedEars' Lahn

R

Richard Cornford

Thomas said:
Your information is inaccurate.

Probably more superficial than inaccurate. To date there have been no
reports of an browsers that actually terminated CDATA SCRIPT element
contents just on the first occurrence of ETAGO, they seem to all wait
for ETAGO followed by (case folded) 'SCRIPT'. But, as usual, a failure
to observe (or observe and report) is not an indicator of the
non-existence of the thing that has not been observed. There may be, in
the past, currently or in the future, a browser that does behave
precisely as described in the HTML 4 specifications and will terminate
CDATA at the very first ETAGO, and HTML authors have no justification
for being surprised when/if that does happen.
CDATA content ends at an ETAGO delimiter,
no matter the element.

http://www.w3.org/TR/html4/types.html#type-cdata

That would be in HTML, according to the HTML speciation. The SGML
specification seems to have a different attitude, where it says (ISO
8879, Appendix B.13.1.1):-

| If an element contains declared character data, it cannot
| contain anything else. The markup parser scans it only to
| locate an ETAGO or NET; other markup is ignored. Only the
| correct end-tag (or that of an element in which this element
| is nested) will be recognised.

- which says that although the parser is looking for ETAGO it is only
interested in them if they start the "correct end-tag", or the end-tag
of a containing element. Given that the closing SCRIPT tag is not
optional (in HTML) it could be suggests that a hypothetical SGML parser
would skip '</'s until it found </script>. On the other hand it may be
that if the opening script was in, say, the HEAD the character sequence
'</head>' might be taken as "that of an element in which this element is
nested" and used to terminate the CDATA.

On the whole it has got to be safest to break up all occurrences of
ETAGO inside CDATA in HTML documents, and so satisfy all interpretations
of all applicable documents.

(It is also worth noting that ISO 8879 Appendix B is explanatory text
not reference.)
Yes, the W3C Validator's SGML parser being one of them.


Maybe that as well.

There is no harm in being conservative here.
It is not necessary to escape ETAGOs if they do not
occur within `script' element CDATA content.

Aren't they just as potentially problematic in any other element's CDATA
contents (STYLE?)
So the said snippet has the
best chance to work everywhere.


Add me.
<snip>

As do I, but not for that reason. It is a habit I acquired writing JSP,
where javascript and Java may appear together. I wanted the two to be as
visual distinct as possible (even though the syntax highlighting rules
for JSP apply the same rules to all code seen) and given that Java
string literals must be delimited with double quotes using single quotes
with javascript meant that if chunks of code contained sequences of text
characters in one color they were Java, and another color meant
javascript. A very immediate indicator of which code context you are
looking at in a JSP.

(Unfortunately, the SGML spec is not available free
of charge at ISO.)
<snip>

Anyone considering paying for a copy of an ISO specification would be
well advised to get their copy in electronic form. Although I much
prefer reading documents from paper (rather than a computer screen) the
quality of the ISO printed documents is not particularly good (read
'quite bad', even (ironically for the ISO) 'substandard') and it would
not take much work to achieve a superior hard copy from a printout of
the electronic version.

Richard.
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top