我正在尝试使用本地服务器在 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");
最佳答案
我们今天刚刚遇到这个问题,因为我们仍然使用 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>
资源
这是来自 Jake Archibald 的另一个资源.
关于javascript - Firefox 不会加载类型为 "module"的脚本标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49470842/