By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,680 Members | 1,666 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,680 IT Pros & Developers. It's quick & easy.

unterminated string constant

P: n/a
From server-side code I'm using Response.Write to display a javascript alert
box. It works fine except when I try to include a new line character, which
causes this javascript error:

"Unterminated string constant"

Here's my c# code:

strText = "Line 1 text\nLine2 text";
Response.Write("<script>alert('" + strText + "');</script>");

Any ideas how to get around this problem?
Oct 19 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Try the "more correct" way.
http://www.codeproject.com/aspnet/ClientServer.asp
Page.RegisterStartupScript

"ken s" <ke**@discussions.microsoft.comwrote in message
news:03**********************************@microsof t.com...
From server-side code I'm using Response.Write to display a javascript
alert
box. It works fine except when I try to include a new line character,
which
causes this javascript error:

"Unterminated string constant"

Here's my c# code:

strText = "Line 1 text\nLine2 text";
Response.Write("<script>alert('" + strText + "');</script>");

Any ideas how to get around this problem?

Oct 19 '06 #2

P: n/a
Thanks for the response. I understand that there are other ways to call a
javascript function. But this way works fine for most things. Do you know
why the new line doesn't work here?
Oct 19 '06 #3

P: n/a
List 1 text should end with '\' character if the string constant continued
on a new line: Try strText = "Line 1 text\\\nLine2 test";

"ken s" <ke**@discussions.microsoft.comwrote in message
news:03**********************************@microsof t.com...
From server-side code I'm using Response.Write to display a javascript alert
box. It works fine except when I try to include a new line character, which
causes this javascript error:

"Unterminated string constant"

Here's my c# code:

strText = "Line 1 text\nLine2 text";
Response.Write("<script>alert('" + strText + "');</script>");

Any ideas how to get around this problem?

Oct 20 '06 #4

P: n/a
Hi,

ken s wrote:
From server-side code I'm using Response.Write to display a javascript alert
box. It works fine except when I try to include a new line character, which
causes this javascript error:

"Unterminated string constant"

Here's my c# code:

strText = "Line 1 text\nLine2 text";
Response.Write("<script>alert('" + strText + "');</script>");

Any ideas how to get around this problem?
Make a view-source on the produced HTML code. You will see this:

<script>alert('Line 1 text
Line2 text');</script>

This is illegal JavaScript. Unlike HTML, JavaScript strings cannot be
spread one more than one line without being closed.

What you want is this:

<script>alert('Line 1 text\nLine 2 text');</script>

which in the code behind translates to:

Response.Write( "<script>alert('Line 1 text\\nLine 2 text');</script>" );

HTH,
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Oct 20 '06 #5

P: n/a
Hi,

Siva M wrote:
List 1 text should end with '\' character if the string constant continued
on a new line: Try strText = "Line 1 text\\\nLine2 test";
Too many '\'.

See my post.
Laurent
--
Laurent Bugnion, GalaSoft
Software engineering: http://www.galasoft-LB.ch
PhotoAlbum: http://www.galasoft-LB.ch/pictures
Support children in Calcutta: http://www.calcutta-espoir.ch
Oct 20 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.