1
<html>
<head>
<script type="module" src="topo_space.js"></script>
</head>
<body onload="main()">
</body>
</html>

topo_space.js:

export function main(){
    const innerHTML = `<svg id="main_svg" width="959" height="704"></svg>`;
    console.log(innerHTML);
    document.body.innerHtml = innerHTML;
}

Getting error:

topo_space.html:6 Uncaught ReferenceError: main is not defined at onload

I want to stick to using module, because the js can itself further import a json as module, which I find extremely cool.

Ludovic Aubert
  • 9,534
  • 4
  • 16
  • 28

1 Answers1

1

Checkout this thread to read more information about this problem:

How to use code from script with type=module

The answer is:

window.main = function main() {
  const innerHTML = `<svg id="main_svg" width="959" height="704"></svg>`;
  console.log(innerHTML);
  document.body.innerHtml = innerHTML;
};
<html>
  <head>
    <script type="module" src="topo_space.js"></script>
  </head>
  <body onload="main()"></body>
</html>
AG_1380
  • 552
  • 5
  • 12
  • Thx, it works (error msg no longer there, main() gets called). For some reason document.body.innerHtml doesn't load. Silently (there is absolutely no error msg in the console). – Ludovic Aubert Aug 08 '22 at 16:00
  • Had to change the body to embed the svg tag on the html side. Then in main() code getElementById("main-svg").innerHTML=innerHTML. And it eventually worked out. So this second issue was minor. – Ludovic Aubert Aug 08 '22 at 16:24