给脚本标签一个 ID [英] Giving the script tag an ID
问题描述
我遇到了一个场景,给 script
元素一个 id
属性可以轻松解决问题.但是,在阅读 w3schools<上的 script
元素后/a> 和 quirksmode,似乎这样做可能会有一些不可预见的后果.
I came across a scenario where giving a script
element an id
attribute would solve a problem easily. However, after reading about the script
element at w3schools and quirksmode, it seems doing so could have some unforeseen consequences.
有没有人在使用 Chrome、Safari、FF3 up 和 IE 7 up 等浏览器时遇到过这些问题?
Has anyone come across any of these issues with browsers such as Chrome, Safari, FF3 up and IE 7 up?
推荐答案
在当前所有浏览器中都很好.
It's fine in all current browsers.
唯一出现 错误的浏览器是 Netscape 4,我们很久很久以前就不再关心它了.
The only browser that got <script id>
wrong was Netscape 4, which we stopped caring about a long, long time ago.
那个 quirksmode 页面似乎已经过时了,它使用了 language
属性、脚本 <!--
隐藏和 application/x-javascript
.它关于避免在 中使用
(并将其放在
中)的建议与今天鼓励的做法.
That quirksmode page seems to be badly out of date, what with its use of language
attributes, script <!--
hiding, and application/x-javascript
. Its advice about avoiding <script>
in the <body>
(and putting it in <head>
instead) is at odds with today's encouraged practices.
如果我们在谈论 属性兼容性问题:
defer
并非在任何地方都有效,所以不要依赖它;charset
并非在任何地方都有效,并且所提供脚本的 Content-Type 上的 charset 参数也无效,因此您的脚本字符集最好与页面匹配;type
应该始终是 text/javascript
而不是编写 RFC 4329 的书呆子希望您使用的非工作替代方法之一.
If we're talking <script>
attribute compatibility problems: defer
doesn't work everywhere so don't rely on it; charset
doesn't work everywhere, and neither does the charset parameter on the served script's Content-Type, so your script charset had better match the page; type
should always be text/javascript
and not one of the non-working alternatives the pedants who wrote RFC 4329 would like you to use.
这篇关于给脚本标签一个 ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!