如果默认为text/javascript,则脚本元素的HTML"nomodule"属性的用途是什么? [英] What’s the purpose of the HTML “nomodule” attribute for script elements if the default is text/javascript?
问题描述
我不清楚为什么支持ES6模块的新浏览器中存在nomodule
属性.
I am not clearly understanding why the nomodule
attribute exists in the new browsers that support ES6 modules.
在HTML 5中,type
属性是可选的,并且默认为
In HTML 5, the type
attribute is optional and defaults to text/javascript
:
type属性提供脚本语言或数据格式.如果存在该属性,则其值必须是有效的MIME类型.不能指定charset参数.如果缺少该属性,则使用默认值,即"text/javascript".
The type attribute gives the language of the script or format of the data. If the attribute is present, its value must be a valid MIME type. The charset parameter must not be specified. The default, which is used if the attribute is absent, is "text/javascript".
它不是默认为<script type="module" src="module.js"></script>
.此默认值是否已更改?如果没有,为什么必须要nomodule
?我可以不使用nomodule
就使用<script src="bundle.js"></script>
吗?
It doesn't default to <script type="module" src="module.js"></script>
. Has this default changed? If not, why would nomodule
be necessary? Can I just use <script src="bundle.js"></script>
without nomodule
?
推荐答案
模块脚本来忽略特定的script
元素:
The purpose of the nomodule
attribute is to cause newer browsers that support module scripts to ignore a particular script
element:
nomodule
属性是一个布尔型属性,可防止脚本在支持模块脚本的用户代理中执行.
The
nomodule
attribute is a boolean attribute that prevents a script from being executed in user agents that support module scripts.
此示例显示了如何包括用于现代用户代理的模块脚本和用于老用户代理的经典脚本:
This example shows how to include a module script for modern user agents, and a classic script for older user agents:
<script type="module" src="app.js"></script>
<script nomodule src="classic-app-bundle.js"></script>
在支持模块脚本的现代用户代理中,具有nomodule
属性的script
元素将被忽略,而具有"module
"类型的script
元素将被忽略.将被提取和评估(作为模块脚本).相反,较旧的用户代理将忽略类型为"module
"的script
元素,因为这对于他们来说是未知的脚本类型-但他们在获取和评估其他script
元素时将没有问题(作为经典脚本),因为它们未实现nomodule
属性.
In modern user agents that support module scripts, the script
element with the nomodule
attribute will be ignored, and the script
element with a type of "module
" will be fetched and evaluated (as a module script). Conversely, older user agents will ignore the script
element with a type of "module
", as that is an unknown script type for them — but they will have no problem fetching and evaluating the other script
element (as a classic script), since they do not implement the nomodule
attribute.
这就是它的工作方式.
在HTML 5中,
type
属性是可选的,默认为text/javascript
...此默认设置是否已更改?
In HTML 5, the
type
attribute is optional and defaults totext/javascript
… Has this default changed?
默认值未更改-仍为text/javascript
.但是type
属性现在也可以具有值module
,这意味着浏览器仍然可以解析并以text/javascript
的身份执行它,而且还可以作为
The default hasn’t changed—it’s still text/javascript
. But the type
attribute can now also have the value module
, which means browsers still parse and execute it as text/javascript
—but also specifically as a module script.
如果没有,为什么需要
nomodule
?
如上例所示,有必要阻止新的支持模块脚本的浏览器执行仅适用于不支持模块脚本的旧浏览器的脚本.
It’s necessary in order to prevent new browsers that support module scripts from executing a script that’s intended only for old browsers that don’t support module scripts, as in the above example.
我可以不使用
nomodule
就使用<script src="bundle.js"></script>
吗?
是-如果bundle.js
不使用模块.如果它使用模块,则需要在其上放置type=module
(在这种情况下,旧的浏览器将忽略它,因为它们无法识别type
的module
值).
Yes—if bundle.js
doesn’t use modules. If it uses modules, you‘d want to put type=module
on it (in which case old browsers will ignore it since they don’t recognize the module
value for type
).
这篇关于如果默认为text/javascript,则脚本元素的HTML"nomodule"属性的用途是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!