[英]String problems with Javascript double quotes inside single
This is my code: 这是我的代码:
<script>
function popupTest(title) {
alert(title);
return false;
}
</script>
<a href="" onclick="return popupTest('This is 'some' "test" string')"><span>Recommend</span></a>
Using Firefox 4 I get the error: 使用Firefox 4我收到错误:
Error: missing ) after argument list
Source Code:
return popupTest('This is 'some' "test" string')
It's like it's decoding the HTML entities but I don't know why. 这就像它解码HTML实体,但我不知道为什么。
Have also tried... 也试过......
<a href="" onclick="return popupTest('This is \'some\' \"test\" string')"><span>Recommend</span></a>
Which gives error: 这给出了错误:
Error: unterminated string literal
Source Code:
return popupTest('This is \'some\' \
'
is HTML for '
. 是
'
HTML '
。 So for the first example the HTML is parsed and the JavaScript engine is passed: 因此,对于第一个示例,解析HTML并传递JavaScript引擎:
return popupTest('This is 'some' "test" string')
… and the second '
terminates the string. ......第二个
'
终止字符串。
On the other hand: 另一方面:
onclick="return popupTest('This is \'some\' \"test\" string')"
Is parsed as: 被解析为:
An onclick attribute with the value
return popupTest('This is \\'some\\' \\
followed by some invalid data.带有值的onclick属性
return popupTest('This is \\'some\\' \\
后跟一些无效数据。
You need to deal with the JavaScript first: 你需要先处理JavaScript:
return popupTest('This is \'some\' "test" string')
and then escape it for HTML: 然后逃脱它的HTML:
onclick="return popupTest('This is \'some\' "test" string')"
You would probably be better off using unobtrusive JavaScript and binding the event handlers with JavaScript instead of using intrinsic event attributes. 您可能最好使用不显眼的JavaScript并使用JavaScript绑定事件处理程序而不是使用内部事件属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.