/    Sign up×
Community /Pin to ProfileBookmark

carriage return in a string?

Hi,
The javascript doesn’t show a string when the string have carriage return and line feed. How could I get around of this?.
Should I add escape character “” before carriage return and line field and then send it to the client?

This string comes from a jsp/java page with carriage return and line feed.

Please advise.
Thanks.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@ExuroAug 10.2004 — The escape code for a carriage return is [FONT=courier new][COLOR=blue]n[/COLOR][/FONT]. You can use this in [FONT=courier new]alert()[/FONT] and other such functions to create new lines. If you're using [FONT=courier new]document.write()[/FONT] though you probably would want to do something more like [FONT=courier new]"text text text [color=blue]<br />n[/color]"[/FONT]. Hope that helps!
Copy linkTweet thisAlerts:
@David_HarrisonAug 10.2004 — If you're using docuement.write() you could just use document.writeln() which will automatically add n to the end of every line.
Copy linkTweet thisAlerts:
@Willy_DuittAug 11.2004 — To be used with a [i]document.write[/i] statement the new line escape characters ([b]n[/b]) need to be wrapped in [b]<pre></pre>[/b] tags elsewise the formatting will be lost and the results will be displayed as one long string when the statements are written to the document. The same thing with using [i]document.writeln[/i] which automatically adds the new line escape characters to the end of each line when looking at the source but will also be displayed as one long string without line breaks when written to and displayed in the document.

For example, you can try the two examples below with the [b]<pre>[/b] lines commented and uncommented...

<i>
</i>&lt;script type="text/javascript"&gt;
document.writeln('&lt;pre&gt;');
document.writeln('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.');
document.writeln('Proin lobortis turpis sodales quam. Curabitur mattis.');
document.writeln('Integer ac justo. Pellentesque a sem ac urna congue dictum.');
document.writeln('Vivamus scelerisque feugiat massa.');
document.writeln('Lorem ipsum dolor sit amet elit.');
document.writeln('Duis lacinia justo eget nibh.');
document.writeln('Maecenas enim.');
document.writeln('Suspendisse magna.');
document.writeln('Phasellus aliquam.');
document.writeln('Donec nec tellus. ');
document.writeln('&lt;/pre&gt;');
&lt;/script&gt;

&lt;script type="text/javascript"&gt;
document.write('&lt;pre&gt;');
document.write('Lorem ipsum dolor sit amet, consectetuer adipiscing elit.n');
document.write('Proin lobortis turpis sodales quam. Curabitur mattis.n');
document.write('Integer ac justo. Pellentesque a sem ac urna congue dictum.n');
document.write('Vivamus scelerisque feugiat massa.n');
document.write('Lorem ipsum dolor sit amet elit.n');
document.write('Duis lacinia justo eget nibh.n');
document.write('Maecenas enim.n');
document.write('Suspendisse magna.n');
document.write('Phasellus aliquam.n');
document.write('Donec nec tellus. n');
document.write('&lt;/pre&gt;');
&lt;/script&gt;


Otherwise, you will need to use [b]<br>[/b] tags to retain new line formatting when the document is parsed...

.....Willy
Copy linkTweet thisAlerts:
@XunilJan 13.2005 — I'm using the script below and when I remove the rn the script works fine, but if they are in place I get "Unterminated string constant" Any way around this? What am I doing wrong?

function verify_info(form) {

var error_msg;

if(form.vendor_id.value == "Select") {

error_msg = "rnA Vendor has not been chosen";

focus_line = "vendor_id";

}

if(error_msg) { alert('The Purchase Order has not been filled out completely' + error_msg); }
return(false);

}[/quote]


Xunil
Copy linkTweet thisAlerts:
@David_HarrisonJan 13.2005 — Try just using n.
Copy linkTweet thisAlerts:
@psmithphilAug 29.2006 — The escape code for a carriage return is [FONT=courier new][COLOR=blue]n[/COLOR][/FONT]. You can use this in [FONT=courier new]alert()[/FONT] and other such functions to create new lines. If you're using [FONT=courier new]document.write()[/FONT] though you probably would want to do something more like [FONT=courier new]"text text text [color=blue]<br />n[/color]"[/FONT]. Hope that helps![/QUOTE]

Exuro, it's two years later but your post solved a problem for me. I didn't know how to make separate lines in a multiple line Response.Write.

Response.Write("{var output; output = '" & waitMessage & "nn" & waitMessage2ndLine & "nn" & waitMessage3rdLine & "';dots++;if(dots>=dotmax)dots=1;")

Thank you for your excellent post!
×

Success!

Help @chadorbaf spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.7,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...