简体   繁体   中英

How use export in an HTML

When I have extern javascript file with an export-statement how can I call this in an HTML ?

  • An import in a <script> tag also doesn't work.
  • an include over the <head> gives me a "Unexpected token export" error

For example: my extern js-File

export function myFunction(text) { return text; }

My HTML:

<!DOCTYPE html>
<html>
<head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8" />
    <script src="myExternFile.js"> </script>
</head>
<body>
    <script>
        console.log(myFunction("some text"));
    </script>
</body>
</html>

According to ECMAScript modules in browsers you can do it with <script type="module">

<script type="module">
  import {myFunction} from './myExternalFile.js';

  console.log(myFunction("some text"));
</script>

Your function will work without the export. Just make sure to call it between the script tags.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM