Solved

Javascript error "missing ) after argument list" when trying to display user text

Posted on 2011-03-09
5
558 Views
Last Modified: 2012-05-11
I'm using ckEditor to getuser text and store it thereafter in Sql Server 2005 database.
In some cases (identified as paste from word) , when trying to display the user input in a jquery dialog, the dialog is not opening, and firebug shows this error:  "missing ) after argument list".
An error example is: "missing ) after argument list; Apple-style-span" style="border-collap...ans: 2; text-indent: 0px; text-transfo"
I have attached the original user input and the code that retrieves the data and display it to enduser.
Please help me with a solution that could "clear" extra html tags.
 Thanks!
user input:
Hello,
This is the first sample text.
This is the second sample text.

what is stored in database:
<p> &nbsp;</p><div style="font-family: Arial,Verdana,sans-serif; font-size: 12px; color: rgb(34, 34, 34); background-color: rgb(255, 255, 255);"> <p>  <span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;">Hello,,</span></p> <p>  <span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;">This is the first sample text..</span></p> <p>  <span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;">This is the second sample text..</span></p> <p>  <span class="Apple-style-span" style="border-collapse: separate; color: rgb(0, 0, 0); font-family: 'Times New Roman'; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px; font-size: medium;">

code to store user input:
string currentNote = CaseTextArea.Text;
HttpUtility.HtmlEncode(currentNote);
--update procedure--

code to get user input and display it in jquery dialog:
 string comments = "";
                cmd.CommandText = "dbo.prc_csGetCaseNotes";
                cmd.Parameters.AddWithValue("@CaseInstanceId", caseInstanceId);
                cmd.CommandType = CommandType.StoredProcedure;
                sqlConn.Open();
                SqlDataReader rdr1 = cmd.ExecuteReader(CommandBehavior.CloseConnection);
                rdr1.Read();
                if (rdr1.HasRows)
                {
                    comments = rdr1["NoteText"].ToString();
                    comments = HttpUtility.HtmlDecode(comments);
                }
                sqlConn.Close(); 
                HttpUtility.HtmlEncode(comments);

System.Text.StringBuilder sbScript = new System.Text.StringBuilder();
                sbScript.Append(@"$(function() {");
                sbScript.Append(@"$('#divShowDetails').append('<br/><b>Comentarii:</b><br/>" + comments + "');");
                sbScript.Append(@"$('#divShowDetails').css('display','block');");
                sbScript.Append(@"$('#divShowDetails').dialog('option','width','750');");
                sbScript.Append(@"$('#divShowDetails').dialog('option','height','500');");
                sbScript.Append(@"$('#divShowDetails').dialog('open');});");                
                if (!Page.ClientScript.IsStartupScriptRegistered("scriptShowOffersComments"))
                    Page.ClientScript.RegisterStartupScript(this.GetType(), "scriptShowOffersComments", sbScript.ToString(), true);

Open in new window

0
Comment
Question by:danielivanov2
  • 2
  • 2
5 Comments
 
LVL 7

Accepted Solution

by:
dxdinh earned 400 total points
ID: 35092835
you have single quote "'" within your comments field

'Times New Roman';

so I would try to escape character

from
                sbScript.Append(@"$('#divShowDetails').append('<br/><b>Comentarii:</b><br/>" + comments + "');");
 ---
to                sbScript.Append(@"$('#divShowDetails').append('<br/><b>Comentarii:</b><br/>" + comments.Replace("'", @"\'") + "');");
 
0
 
LVL 20

Expert Comment

by:Proculopsis
ID: 35093508

//Try something like this for better readability:

sbScript.Append(@"
  $(function() {
    $('#divShowDetails').append('<br/><b>Comentarii:</b><br/>" + comments + "');
    $('#divShowDetails').css('display','block');
    $('#divShowDetails').dialog('option','width','750');
    $('#divShowDetails').dialog('option','height','500');
    $('#divShowDetails').dialog('open');
  });
");

0
 
LVL 20

Expert Comment

by:Proculopsis
ID: 35093613

Of course you will need EncodeJsString( comments ) as well.

public static string EncodeJsString(string s) 
{ 
StringBuilder sb = new StringBuilder(); 
foreach (char c in s) 
{ 
switch (c) 
{ 
case '\'': 
sb.Append("\\\'"); 
break; 
case '\"': 
sb.Append("\\\""); 
break; 
case '\\': 
sb.Append("\\\\"); 
break; 
case '\b': 
sb.Append("\\b"); 
break; 
case '\f': 
sb.Append("\\f"); 
break; 
case '\n': 
sb.Append("\\n"); 
break; 
case '\r': 
sb.Append("\\r"); 
break; 
case '\t': 
sb.Append("\\t"); 
break; 
default: 
int i = (int)c; 
if (i < 32 || i > 127) 
{ 
sb.AppendFormat("\\u{0:X04}", i); 
} 
else 
{ 
sb.Append(c); 
} 
break; 
} 
} 

return sb.ToString(); 
}

Open in new window


[ref]
0
 

Author Comment

by:danielivanov2
ID: 35096462
I have put in ckeditor the string detalied in first post (what was stored in database), modified the script to encode js string:
 string currentNote = CaseTextArea.Text;
            EncodeJsString(currentNote);
            HttpUtility.HtmlEncode(currentNote);
but the result is aproximately the same: cannot open dialog, javascript error:

missing ) after argument list
[Break On This Error] na,sans-serif; font-size: 12px; color:...; font-style: normal; font-variant: no
0
 

Author Closing Comment

by:danielivanov2
ID: 35106193
It worked by removing single quote, that was the problem
thanks!
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've been asked to discuss some of the UX activities that I'm using with my team. Here I will share some details about how we approach UX projects.
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmakerā€¦
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now