Javascript 是否仍应“隐藏”?在 HTML 注释标签中?
我最近继承了一些 Web 代码,发现所有 Java Script 脚本都包含在 HTML 注释标记中
例如:
<script type="text/javascript" language="javascript"><!--
function ValidateForm() { ... }//-->
据我了解,此方法阻止较旧的、不受支持的浏览器解释您的 Java Script。然而,这不是我曾经被教导要做的事情,我想知道现在是否认为这是不必要的,或者这仍然是一种常见的做法?如果是这样,为什么?
更新:感谢kennebec提出的将它们保留在其中的建议,我现在已经这样做了,同时也感谢Emmett:我一定会把它们保留在我将来编写的任何代码中!
I have recently inherited some web code and found that all the Java Script scripts are contained within HTML comment tags
For example:
<script type="text/javascript" language="javascript"><!--
function ValidateForm() { ... }//-->
As I understand it, this method prevented older, non-supported, browsers from interpreting your Java Script. However this is not something I was ever taught to do, and I wonder if this is now considered to be unnecessary, or is this still a common practice? If so, why?
Update: Thanks to kennebec for your advice to leave them in, which I have done for now, and thanks to Emmett also: I'll be sure to leave them out of any future code I write!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
http://javascript.crockford.com/script.html:
http://javascript.crockford.com/script.html:
这是因为 XHTML 验证器。 js 代码周围的 HTML 注释应使用外部标签。验证器应该查看你的 html,而不是你的 js。
强烈推荐这篇文字
https://developer.mozilla.org/en/properly_using_css_and_javascript_in_xhtml_documents
关于这个主题的所有内容都在哪里。
It is because of XHTML validator. HTML comments around js code should used outside tag. The validator is supposed to look at your html, not your js.
I strongly recommended this text
https://developer.mozilla.org/en/properly_using_css_and_javascript_in_xhtml_documents
where is everything about this topic.
我的网站是XHTML 1.1,也就是XML。
我的 JavaScript 被封装在
// 和
//]]>
,如果存在 XML 实体(例如 '&'、'<' 或'>')在代码中。My website is XHTML 1.1, which is XML.
My JavaScript is wrapped in
//<![CDATA[
and//]]>
, if there is an XML entity (such as '&', '<', or '>') in the code.当
时,删除
。
例如,
如果没有
,将以不同的方式进行解析。
因此,请继续删除它们,但先检查一下。
顺便说一句,在
script
元素内,它们不被称为“注释”,它们被称为“转义文本跨度”。来自 http://www.w3.org/ TR/2009/WD-html5-20090423/syntax.html#syntax-escape
Removing the
<!--
could cause a script to fail when there is a</script
as part of the program text inside the<!-- ... -->
.For example,
will parse differently without the
<!-- ... -->
.So go ahead and remove them, but check first.
Btw, inside
script
elements, they're not called "comments", they're called "escaping text spans".From http://www.w3.org/TR/2009/WD-html5-20090423/syntax.html#syntax-escape