Home > Software design >  Javascript not loading in when importing other script
Javascript not loading in when importing other script

Time:09-27

I have a JavaScript file I've linked to an html file. The file links fine whenever I don't have anything imported into it, but whenever I import another file into it, it isn't even loaded.

What the JavaScript file looks like when it loads in.
Example of script loading in.

//import './the_story/project.js';

function savE(){
    alert("Saved!");
}

function loaD(){
    alert("Loaded!")
}

What the JavaScript file looks like when it doesn't load in.
Example of the script not loading in.

import './the_story/project.js';

function savE(){
    alert("Saved!");
}

function loaD(){
    alert("Loaded!")
}

I'm honestly pretty stumped on this. I've tried having the script inside the html file itself and it still didn't work. It claimed my functions were undefined.

CodePudding user response:

In your html import your javascript file as module like so

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

for futher reading about normal script file and module please read official docs

CodePudding user response:

Did your script tag have type="module"?

  • Related