【问题标题】:Firefox won't load script tag of type "module"Firefox 不会加载“模块”类型的脚本标签
【发布时间】:2021-01-15 00:24:07
【问题描述】:

我正在尝试使用本地服务器在 Firefox 中运行 ES6 JavaScript。因此,我需要使用"module" 类型的脚本标签,而不是"text/javascript"。以下适用于 Chrome,但不适用于 Firefox。帮忙?

index.html

<!DOCTYPE html>
<html>
<head>
<script type="module" src="main.js"></script>
</head>
<body>
</body>
</html>

main.js

console.log("Hello World");

【问题讨论】:

  • 您依赖的是全新的实验性功能。您是否探索过更标准的方法,例如捆绑?

标签: javascript html firefox ecmascript-6


【解决方案1】:

我们今天刚刚遇到这个问题,因为我们仍在使用 Firefox ESR 52 并且尚未迁移到 ESR 60。

在 Firefox 60 中,此功能默认启用。 Check out the Firefox 60 release notes.

在版本 54 到 59 中,您可以通过 about:config 并设置 dom.moduleScripts.enabled 来启用它。 Noted in the script element docs (implementation notes drop down).

支持 ES6 模块的浏览器将忽略带有 nomodule 的脚本。所以你可以有一个没有模块的脚本作为后备。像这样:

    <!-- in case ES6 modules are supported -->
    <script type="module" src="main.js"></script>
    <!-- in case ES6 modules aren't supported -->
    <script src="main-fallback.js" defer nomodule></script>

资源

以上建议引用自herehere

这是来自Jake Archibald 的另一个资源。

【讨论】:

    【解决方案2】:

    https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script

    对于 type.module,在 FF 上:用户必须明确启用此功能。

    此功能在 dom.moduleScripts.enabled 首选项后面(需要设置为 true)。要更改 Firefox 中的首选项,请访问 about:config。

    【讨论】:

      【解决方案3】:

      我刚刚解决了同样的问题。

      <script type="module" src="main.js"></script>
      

      应该改为

      <script type=module src="main.js"></script>
      

      type 属性 module 不应被 " "

      包裹

      在这种情况下,es6 模块可以在 Chrome、Edge、Opera 和 Firefox 中运行。

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-30
      • 1970-01-01
      • 2015-07-27
      • 2020-04-22
      • 2016-03-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多