Javascript code just stopped working?

https://school-planner.glitch.me/

Title says it all. Just now my JS script has just stopped working. For seemingly no reason. No clue what I edited to cause this but it has happened. Help would be appreciated.

If you look at your browser’s developer tools “console”, it’s showing the following error:

Uncaught SyntaxError: Cannot use import statement outside a module

Which usually means you forgot to add type="module" to your <script src="..."> tag and so the browser’s trying to load your JS without support for the import keyword.

1 Like

tl;dr replace

<script src="script.js"></script>

with

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

in index.html

You’ll also need to add

window.getData = getData;

at the bottom of the script.js file in order to make the onclick handler work.

Note that it’s generally a really bad idea to put things on window because you might have just overwritten something that already exists, but that you didn’t know was part of one of the thousand-or-so Web APIs. Instead, when you’re dealing with modules, you can just export anything you need to pass around.

Thanks! That fixed it!

1 Like

Yeah I know but adding it to window is easier and you can easily check if something is part of Window by using your devtools.

Anyway you should almost never use onclick() handlers, but in this case it’s easier

Thankyou so much for this solution. Its help me.

I see another problem with the code:

In the code, I see that you said age = age + 2, where age is originally text from the input. Due to some weird reasons, JavaScript treats the input as a string, and you can concatenate numbers with a string so it becomes a longer string (in this case '100' + 2 = '1002'.
I would assume that if you want to just add 2, the correct way to write it is age = +age + 2, as it converts age to a number before adding.
Hope this helps!