<script type="mime" src="...">除了JS还有什么有用的mime吗?

Is there any useful mime for <script type="mime" src="..."> except for JS?

通过大量阅读我得出结论:

<script>标签可以存储数据,但只有在不使用src的情况下才能访问。

我想使用 src,因为我的脚本加载器就是这样工作的。 type 属性和每个 mime-types 是没用还是什么?...

我发现 "text/javascript"(与 "application/javascript" 相同)不能被 .innerText.innerHTML.toString 访问(希望我错过了这里有东西)。 但它可以执行,这是唯一可以在没有浏览器扩展的情况下使用外部 <script> 完成的事情 - 我猜。因为使用了 src,所以无法访问数据?

我希望我是错的,因为我想扩展我的脚本加载器来加载可以读取的 mime 类型。有没有?请告诉我any/some 可从外部动态加载的可访问 MIME 类型。


更新 - 谢谢你的回答 - 现在我得出结论:

根据 whatwg.org

,模式 <script type src> 对除 Javascript 以外的任何 MIME 类型都没有用

Setting the attribute to any other value means that the script is a data block, which is not processed. None of the script attributes (except type itself) have any effect on data blocks.

因此,将 type 设置为 Javascript 以外的任何其他值会使 src 无效并且 <script> 成为 数据块 没那么有用,因为变量作为将数据放入其中的唯一方法应该更好地将数据存储到 div 中。

永远不会 typesrcscript 中一起工作(这会带来安全风险),因为 importfetch 已经加载文件的新方法。在没有 CORS 的服务器上无法加载 E6 导入的情况下,仍可使用脚本加载器。

不是特别重要,除非您有 .json 文件并且服务器在响应中发送适当的 MIME 类型 header。否则,<script>标签的内容将不会被处理。

根据 WHATWG specification

The type attribute allows customization of the type of script represented:

  • Omitting the attribute, setting it to the empty string, or setting it to a JavaScript MIME type essence match, means that the script is a classic script, to be interpreted according to the JavaScript Script top-level production. Classic scripts are affected by the async and defer attributes, but only when the src attribute is set. Authors should omit the type attribute instead of redundantly setting it.

  • Setting the attribute to an ASCII case-insensitive match for the string "module" means that the script is a module script. If it has a JavaScript MIME type, or if the script is embedded inline, then it will be interpreted as a JavaScript module script according to the JavaScript Module top-level production; if it has a JSON MIME type, then it will be interpreted as a JSON module script. Module scripts are not affected by the defer attribute, but are affected by the async attribute (regardless of the state of the src attribute).

  • Setting the attribute to any other value means that the script is a data block, which is not processed. None of the script attributes (except type itself) have any effect on data blocks. Authors must use a valid MIME type string that is not a JavaScript MIME type essence match to denote data blocks.

A JavaScript MIME 定义为以下任何一项:

  • application/ecmascript
  • application/javascript
  • application/x-ecmascript
  • application/x-javascript
  • text/ecmascript
  • text/javascript
  • text/javascript1.0
  • text/javascript1.1
  • text/javascript1.2
  • text/javascript1.3
  • text/javascript1.4
  • text/javascript1.5
  • text/jscript
  • text/livescript
  • text/x-ecmascript
  • text/x-javascript

一个JSON MIME定义为

any MIME type whose subtype ends in "+json" or whose essence is "application/json" or "text/json".

请务必注意,您必须使用有效的 MIME 类型,以保证浏览器永远不会尝试处理它。否则,未来对规范的补充可能会破坏您的代码:

The requirement that data blocks must be denoted using a valid MIME type string is in place to avoid potential future collisions. If this specification ever adds additional types of script, they will be triggered by setting the type attribute to something which is not a MIME type, like how the "module" value denotes module scripts. By using a valid MIME type string now, you ensure that your data block will not ever be reinterpreted as a different script type, even in future user agents.

WHATWG here.

定义了有效的 MIME 类型