473,406 Members | 2,369 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

unterminated string constant

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
5 13716
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: noViagraHere | last post by:
What do i need to add to the document line to stop getting an Unterminated String Constant error? itemtocheck = '1300'; document.writeln('<!--#include...
5
by: KathyB | last post by:
Hi, this is the first lines of a function. Although it runs, it still throws an "Unterminated string constant" error in the browser. It is all in one line, just wouldn't fit here. The error...
6
by: Jeff | last post by:
Hi, does anyone know why this: <a onclick="insertatcaret(window.opener.document.formname.fieldname,'<td class="header">')">text</a> returns a "Unterminated String Constant" error message in IE...
3
by: aroraamit81 | last post by:
Hi, I am facing a problem in javascript. strdata is a variable of javascript and following is an assignment( hmmmmmmmmmmm a Huge one but unfortunately its not mine code, I am just trying to...
18
by: William | last post by:
I have the following javascript function that updates a scroll_list and sends the updated entry (with its index) to a server script ( i.e. http://mkmxg00/cgi/confirmUpload.pl ) for further...
2
by: polilop | last post by:
When i open my page in IE it shows an error Unterminated string constant om Line..... When i look at the line it shows the line where the </SCRIPT> tag is ???? Moziila dose not see this error,...
2
by: rajuk | last post by:
Hi i have following code,when i execute this code i got unterminated string constant error.any javascript guru can you look into this please. raju /* The link details */ var links = new Array...
1
by: VUNETdotUS | last post by:
Let's say I have a string: div.innerHTML = "<a onclick='foo(\""+myWord+"\");'></a>"; in IE only (tested version 7) if var myWord = "English" then it works fine but if var myWord = "Modifier...
1
by: buntyindia | last post by:
I am getting the following string from the database using Java Text <p>hi m</p> <p>Do Something</p> <p>&nbsp;</p> that I have to load into a javascript based rich text editor.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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
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...

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.