โญES module

JS โŸฉ module โŸฉ ES module

โญ๏ธ ES6 (2015)

ES modues are supported by browsers and servers, with a bit of configuration๏ผš

<!-- โญ๏ธ HTML (browser, client-side JS) -->
<!--    โ•ญโ”€โ”€โ”€โ”€ โญ๏ธ โ”€โ”€โ”€โ•ฎ (module mode)    -->
<script type="module" src="script.js"></script>
// โญ๏ธ (Node.js, server-side JS)
// - add this line to "package.json" file
"type": "module",

async scripts

  • async scripts run immediately when ready, independently of other scripts or the HTML document.

  • async has no effect on inline normal scripts. For module scripts, it works on inline scripts as well. (๐Ÿ‘‰๐Ÿป See: tab 2๏ธโƒฃ above)

Cross-Origin Scripts

Scripts fetched from another origin (e.g. another site) require CORS headers, as described in the chapter Fetch: Cross-Origin Requests. ๐Ÿ“— JS.info โŸฉ External scripts

<!-- another-site.com must supply Access-Control-Allow-Origin -->
<!-- otherwise, the script won't execute -->
<script type="module" src="http://another-site.com/their.js"></script>

Last updated