Formatting For Javascript

Certain characters in your data will cause Javascript to crash. Special characters need to be handled with your server-side code. Below is a C# function that I create to do just such task.
protected string FormatForJS(object input) {
  string data = input.ToString();
  // cast the input to a string 
  data = data.Trim();
  // replace those characters that will crash JAVASCRIPT 
  data = data.Replace("'", "\\'");
  data = data.Replace("\n", "");
  data = data.Replace("\r", "");
  return data;

Post a Comment


Close Menu