【问题标题】:Do I still need to use type='text/javascript'? [duplicate]我还需要使用 type='text/javascript' 吗? [复制]
【发布时间】:2012-08-28 12:08:43
【问题描述】:

可能重复:
Why write <script type=“text/javascript”> when the mime type is set by the server?

我知道&lt;script type='type/javascript'&gt;&lt;/script&gt; 不再需要 type 属性了。我的问题是,我自己插入脚本的时候,还需要用吗?

var hello = document.createElement('script');
hello.type = 'text/javascript'; // still needed?
hello.async = true;
hello.src = 'http://mysite.com/script.js';
(document.getElementsByTagName('head')[0] || document.getElementsByTagName('body')[0]).appendChild(hello);

我看到在 Google Analytics 中他们仍然使用它ga.type = 'text/javascript';,Disqus 也使用它和dsq.type = 'text/javascript';

那么,这有必要吗?

【问题讨论】:

标签: javascript jquery html dom


【解决方案1】:

没有,如果属性不存在,那是默认值:

来自specification

type 属性给出了脚本的语言或格式 数据。如果属性存在,其值必须是有效的 MIME 类型。不得指定 charset 参数。默认值,即 如果该属性不存在,则使用“text/javascript”。

【讨论】:

  • 请注意,这是 HTML5 规范。请说明第一个指定此类默认值的版本。​​
猜你喜欢
  • 2014-03-01
  • 2021-10-24
  • 2014-06-15
  • 2012-04-17
  • 2011-11-17
  • 2012-12-28
  • 2011-10-12
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多