在动态创建脚本时,我们还需要"script.type ='text/javascript"吗?

Zo7*_*o72 11 javascript

代码是这样的:

var script = document.createElement('script');
//script.type = 'text/javascript'; // do I need this ?
script.src = src;
document.body.appendChild(script);
Run Code Online (Sandbox Code Playgroud)

第二行已被注释掉,因为它没有任何区别.或者我错过了什么?

谢谢,

Rob*_*b W 9

否:默认值type已设置为JavaScript("text/javascript").type属性是SCRIPT标记的一个属性,允许Vbscript,例如,只有IE支持.

type属性在HTML5中也是可选的.这可能是一种省略元素type属性的鼓励script.


pun*_*lly 8

不.它可能在html4中并不完全正确,但它不会给你带来任何问题.

HTML5规范

"The type attribute gives the language of the script or format of the data..." 
"...The default, which is used if the attribute is absent, is 'text/javascript'."
Run Code Online (Sandbox Code Playgroud)

我唯一注意到的是,当您未指定类型时,IDE有时不会使用正确的语法突出显示.我在Coda for Mac中找到了这个,这是我现在把它放进去的唯一原因.