嵌入式javascript中的特殊字符 [英] special characters in embedded javascript

查看:29
本文介绍了嵌入式javascript中的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些像这样嵌入到 html 文件中的 javascript

I have some javascript that is embedded in the html file like this

<html>
    <head>
        <script>
            // and the script runs here
        </script>
    </head>
    <body></body>
</html>

它有一条这样的线

if((os == 'Mac')&&((br == 'Safari')||(br == 'Chrome')))

一切正常(这意味着脚本可以正常工作),但是验证器在尖叫:

Everything goes OK (which means the script works), but the validator is screaming:

... 字符&"是分隔符的第一个字符,但作为数据出现
...在以下几种情况下可能会出现此消息:
- 您试图包含<"页面中的字符:您应该将其转义为<"
- 您使用了未转义的&符号&":这在某些情况下可能有效,但建议使用&",这始终是安全的.
- 另一种可能是您忘记关闭前一个标签中的引号.

... character "&" is the first character of a delimiter but occurred as data
... This message may appear in several cases:
-You tried to include the "<" character in your page: you should escape it as "<"
-You used an unescaped ampersand "&": this may be valid in some contexts, but it is recommended to use "&", which is always safe.
-Another possibility is that you forgot to close quotes in a previous tag.

替换所有 & 似乎很荒谬与 &amp.我怎样才能摆脱那个错误?

It seems ridiculous to replace all & with &amp. How can I get rid of that error?

推荐答案

如果您使用 XHTML,则需要将内联脚本包含在 CDATA 部分:

If you are using XHTML, you need to enclose your inline scripts within a CDATA section:

<script type="text/javascript">
// <![CDATA[
    document.write("&");
// ]]>
</script>

进一步阅读:

这篇关于嵌入式javascript中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆