简体   繁体   中英

Escape variable for JavaScript in asp.net

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?

考虑使用Microsoft反跨站点脚本库中JavaScriptEncode方法。

您可以执行lastName.Replace(“'”,“ \\”“)。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM