3

Modern JavaScript allows defining modules right on the Html page:

 </body>
    <script type="module">
        export let Var0 = 2;
    </script>
    <script type="module">
        import { Var0 } from "";
    </script>    
 </body>

Browser (in my case Chrome) does not complain on the type="module" specifier. Without this specifier it complains on the export statement. First script passes without errors. It looks like a module. But how can I import anything from it? Inline or external script requires the module name that I should specify in the import statement.

Is there any way to give a name to a <script> section?

Web-Intern
  • 223
  • 2
  • 13
  • you might want to be a little more specific. For the syntax you're using, it's implied there's some building step. Using Webpack or Parcel or something like that. Because web browsers don't know about import/export syntax Also, If you're including the javascript in a HTML file, you don't need to export. Just add a source tag, then the variables have global scope – Julian Nov 13 '19 at 18:48
  • @Julian, why do you think that they do not know? Why any building step is needed? I just tried importing var/func from external file to an inline script like above and this works fine.... – Web-Intern Nov 13 '19 at 18:50
  • What about creating one script tag and put all js there? Why would you ever want to create two separate inlined modules? – marzelin Nov 13 '19 at 19:11
  • in normal html css js, this code will not work. Export is an ES6 feature that needs a transpiler. So the environment doesn't make sense to me because if you're really just looking to grab variables from different JS files, you can just reference them in the files since when you use the variables and such are global scope. You don't need to use import to do what you're doing – Julian Nov 13 '19 at 19:24
  • related to this question: https://stackoverflow.com/questions/43817297/inlining-ecmascript-modules-in-html – Alireza Esfahani Nov 13 '19 at 19:36

0 Answers0