[英]Escape '@' character in Razor inside variable passed to javascript
I have to pass email in variable to javascript. 我必须将电子邮件中的变量传递给javascript。 Something like this:
像这样:
<button onclick="Send(email)">Send</button>
Obviously there is '@' character in email variable which I need to escape because I am getting this error: "Unexpected token ILLEGAL" 显然,电子邮件变量中有'@'字符,我需要对此字符进行转义,因为我收到此错误:“意外的令牌非法”
I read these posts: Escape @ character in razor view engine and Escape @ character in razor view engine but none of them resolve my problem. 我读了这些帖子: 在razor视图引擎中使用 Escape @字符,在razor视图引擎中使用Escape @字符,但是它们都无法解决我的问题。 I don't know, what I am doing wrong.
我不知道我在做什么错。
There is almost everything what I tried with these posts: 这些帖子几乎涵盖了我尝试的所有内容:
<button onclick="Send(@email)">Send</button>
Result: as expected something@something.com => same error 结果:按预期方式something@something.com =>相同的错误
{
var str = email.Replace("@", ("@@"));
}
<button onclick="Send(@email)">Send</button>
Result: something@@something.com => same error 结果:something @@ something.com =>相同的错误
<button onclick="Send(@:@email)">Send</button>
Result: Server error: ":" is not valid at the start of a code block 结果:服务器错误:“:”在代码块的开头无效
{
var str = email.Replace("@", "@"));
}
<button onclick="Send(@email)">Send</button>
Result: something&64;something.com => Unexpected token ILLEGAL 结果:something&64; something.com =>意外令牌非法
<button onclick="Send(@(email))">Send</button>
or 要么
<button onclick="Send(@(@email))">Send</button>
Result: as expected something@something.com => same error 结果:按预期方式something@something.com =>相同的错误
<button onclick="Send(@@email)">Send</button>
Result: Close one. 结果:关闭一个。 '@' is escaped and this is rendering onclick="@email", but unfortunately not inside the variable email.
'@'被转义,这将呈现onclick =“ @ email”,但不幸的是不在变量email中。
I tried other various option but nothing worked. 我尝试了其他各种选择,但没有任何效果。
<text></text>
and HttpUtility.Encode HttpUtility.JavascriptStringEncode HttpUtility.HtmlAttributeEncode Http.Raw does't seems to work either. 和HttpUtility.Encode HttpUtility.JavascriptStringEncode HttpUtility.HtmlAttributeEncode Http.Raw也不起作用。
I will be glad for any advice. 我将很高兴为您提供任何建议。 Really want to know what I am missing here.
真的想知道我在这里想念的是什么。 Thank you.
谢谢。
I think your issue is not one of Razor and instead JavaScript. 我认为您的问题不是Razor之一,而是JavaScript。 It looks like you need quotes around
@email
because it is a string and JavaScript is complaining about the content without that because it is illegal. 看来您需要在
@email
周围@email
引号,因为它是一个字符串,而JavaScript正在抱怨没有该内容的内容,因为它是非法的。
<button onclick="Send('@email')">Send</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.