Link to home
Start Free TrialLog in
Avatar of danielivanov2
danielivanov2Flag for Romania

asked on

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

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

ASKER CERTIFIED SOLUTION
Avatar of dxdinh
dxdinh
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Proculopsis
Proculopsis


//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');
  });
");


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]
Avatar of danielivanov2

ASKER

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
It worked by removing single quote, that was the problem
thanks!