0

I have the following code in asp.net:

string backToParentFunc = string.Format("backToParent('{0}', '{1}', '{2}', '{3}');",
                                              Server.UrlEncode(login),
                                              Server.UrlEncode(firstName),
                                              Server.UrlEncode(lastName),
                                              Server.UrlEncode(email);
ScriptManager.RegisterStartupScript(this, GetType(), "backToParent", backToParentFunc, true);

Of course it stops working when the "lastName" variable contains ['] symbol (O'Connell). How to correctly escape strings in asp.net to make them appear correctly in JavaScript code?

2 Answers 2

4

Consider using the JavaScriptEncode method from the Microsoft Anti-Cross Site Scripting Library.

Sign up to request clarification or add additional context in comments.

Comments

0

You can do a lastName.Replace("'", "\"").

2 Comments

thanks, but I'm looking for a generic escaping method, not only for ['] symbol.
You have to escape the escape, \\' not just \'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.