What is the "module" value in the type property of the HTML script element

3

Recently I saw a code that when adding a JavaScript file was set to the attribute type with the value module as in the example below:

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

I would like to know what this value is for, therefore, this question brings another within itself:

  • Browser Compatibility
asked by anonymous 07.03.2018 / 13:29

2 answers

3

JavaScript modules allow a program to be split into multiple declaration sequences. Each module explicitly identifies statements that it uses that need to be provided by other modules and which of its statements are available for use by other modules.

In HTML5 compliant browsers the code is treated as a JavaScript module. The content processing of script is not affected by the charset and defer attributes. For information on using the module see here: link

Chrome documentation on the subject: link

Mozilla documentation on the subject: link

Demo: link

About browser compatibility check out the Standards: link ( last updated March 7, 2018)

link

    
07.03.2018 / 14:12
3

module is new feature in ECMAScript 6th edition (ES6). When you set the type="module" attribute to a .js file, you convert it to módulo .

A module is a normal JS file, but with some quirks:

  • The code in a module automatically gains "use strict"; , even if you do not define it in it;
  • You can use the import and export methods in the modules to swap objects such as function , class , var , let , or const .

Everything that is declared inside a module, by default, is local to the module. If you want something declared in a module to be public, so that other modules can use it, you must export this feature ( export ), and for another module to use it, you must import it ( import ).

For compatibility, MDN shows the next table :

On the same page you are informed:

  

This feature is only beginning to be implemented in browsers   natively at this time. It is implemented in many transpilers, such as   TypeScript and Babel, and bundlers such as Rollup, Webpack and Parcel.

Free translation:

  

This feature is only beginning to be implemented natively   in browsers right now. It is implemented in several   such as TypeScript and Babel, and groupers such as Rollup,   Webpack and Parcel.

    
07.03.2018 / 14:35