2

可能重复:
当服务器设置 mime 类型时,为什么要写 <script type=“text/javascript”>?

我知道<script type='type/javascript'></script>不再需要 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';

那么,这有必要吗?

4

1 回答 1

2

不,这是默认值,如果该属性不存在:

规范

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

于 2012-09-03T19:44:11.750 回答