简体   繁体   English

将带双引号和单引号的字符串从ASP传递到javascript函数

[英]Pass a string with double and single quotes from asp to javascript function

I have the following code: 我有以下代码:

PropertyID = 101
PropertyName = "My'complex" property name"" //This is passed from sql query
Response.Write "<a href=""javascript:RenameFunc("& PropertyID & ", '" & PropertyName  & "' )"" onclick=""""></a>"

The problem is that If I have single or double quotes in my variables, the javascript function brakes (which is normal). 问题是,如果变量中有单引号或双引号,则javascript函数会刹车(这是正常的)。

I tried escaping them like this: 我试图这样逃避他们:

 Response.Write "<a href=""javascript:RenameFunc("& PropertyID & ", '" & Replace(Replace(PropertyName,"'",""),"""","")  & "' )"" onclick=""""> </a>"

This doesn't break the function, but it removes the quotes from my string and I need them. 这不会破坏函数,但会从字符串中删除引号,而我需要它们。

I also tried with String.row like this: 我也尝试使用String.row这样:

Response.Write "<a href=""javascript:RenameFunc("& PropertyID & ", String.raw`"& PropertyName &"` )"" onclick=""""> </a>"

Still no success, but maybe I use it the wrong way. 仍然没有成功,但是也许我用错了方式。

Is there any other way to pass the string and keep it as it is, without breaking the javascript function? 还有其他方法可以传递字符串并将其保持不变,而不会破坏javascript函数吗?

Use the Server.HTMLEncode() helper: 使用Server.HTMLEncode()帮助器:

Response.Write "<a href=""javascript:RenameFunc("& PropertyID & ", '" & Server.HtmlEncode(Replace(PropertyName,"'","\'")) & "')"" onclick=""""> </a>"

See Documentation 请参阅说明文件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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