Solved

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

Posted on 2011-03-09
5
549 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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Envision that you are chipping away at another e-business site with a team of pundit developers and designers. Everything seems, by all accounts, to be going easily.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
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…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

705 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

22 Experts available now in Live!

Get 1:1 Help Now