Solved

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

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

Suggested Solutions

"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Color can increase conversions, create feelings of warmth or even incite people to get behind a cause. If you want your website to really impact site visitors, then it is vital to consider the impact color has on them.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

777 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