-1

I am calling JavaScript function which has two parameter. Everything working fine, but when any parameter having single invited comma its not working. I have tried to replace it with \' but still not working. My codes are.

Server Side :

string param1 = "IFES";
string param2 = "IFES Chapter's Introduction";
string str = param2.Replace("'","\'");
btnShow.Attributes.Add("onclick","ShowDetails('" + param1 + "','" + str + "')");

JavaScript Code:

function ShowDetails(prm1,prm2){
     // My code here
}
Raghubar
  • 2,768
  • 1
  • 21
  • 31
  • 3
    I'd rather use JSON'ing instead of adjusting the params here. Still, replace for `\\'` instead should do. – raina77ow Sep 22 '15 at 12:18
  • Did you see http://stackoverflow.com/questions/2083754/why-shouldnt-apos-be-used-to-escape-single-quotes or http://stackoverflow.com/questions/2004168/escape-quotes-in-javascript? – Olivier De Meulder Sep 22 '15 at 12:20

1 Answers1

4

The single backslash is being eaten by the compiler; you need to double up

string str = param2.Replace("'","\\'");
Richard Schneider
  • 34,944
  • 9
  • 57
  • 73