Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<script type=text/javaScript> vs <script type=module>

What is the difference between loading JavaScript files in HTML using <script type=text/javascript> and <script type=module>?

like image 312
Henok Tesfaye Avatar asked Jul 19 '18 09:07

Henok Tesfaye


People also ask

What is the difference between a script and a module in JavaScript?

One script is one module. As simple as that. Modules can load each other and use special directives export and import to interchange functionality, call functions of one module from another one: export keyword labels variables and functions that should be accessible from outside the current module.

What is script type text JavaScript?

scripttype. Specifies the type of the script. Some common values: A JavaScript MIME type like: application/javascript (default) or application/ecmascript.

What is script type module?

Feature: JavaScript modules: <script type=module>Each module explicitly identifies declarations it uses that need to be provided by other modules and which of its declarations are available for use by other modules. <script type=module> allows loading of JavaScript modules inside web pages.

What is module script in JavaScript?

A JavaScript module is a file that allows you to export its code. This allows other JavaScript files to import and use the exported code as their dependencies.


1 Answers

The HTML5 specification discourages the use of type=text/javascript:

From https://www.w3.org/TR/html5/semantics-scripting.html#element-attrdef-script-type:

Omitting the (type) attribute, or setting it to a JavaScript MIME type, 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 charset, async, and defer attributes. Authors should omit the attribute, instead of redundantly giving a JavaScript MIME type.

Setting the attribute to an ASCII case-insensitive match for the string "module" means that the script is a module script, to be interpreted according to the JavaScript Module top-level production. Module scripts are not affected by the charset and defer attributes.

(emphasis by me)

like image 74
Adrian W Avatar answered Nov 15 '22 15:11

Adrian W