Webdev beginner mistake - using javascript function in html?

530208136:
Tansir1

I'm trying to learn the first steps of web development with plain html and javascript. I feel like I'm missing something completely fundamental. I have two files in a directory that is being served up with python's built-in basic webserver: python -m http.server.

My browser throws this error when loading the webpage: Uncaught ReferenceError: myJavascriptFunc is not defined.

Here are my two files.

index.html

<!DOCTYPE html>
<html>
  <head>
    <script type="module" src="./index.js"></script>
  </head>
  <body>
    <p>Hello!</p>
    <script>
      myJavascriptFunc();
    </script>
  </body>
</html>

index.js

export function myJavascriptFunc() {
    console.log("inside myJavascriptFunc");
}

I thought that since I declared my javascript file as module in the head section that it would get loaded before the body element was processed...so it should be defined. I've also made sure to export the myJavascriptFunc function from the script file. What basic mistake did I make?

Edit: Based on Unmitigated's answer I edited the index.html file as follows and everything works as expected.

<!DOCTYPE html>
<html>
  <head>
     <!-- The script element here was removed. -->
  </head>
  <body>
    <p>Hello!</p>
    <script type="module">
      // This script tag has had the src attribute removed.

      import { myJavascriptFunc } from './index.js';
      myJavascriptFunc();
    </script>
  </body>
</html>

Edit 2: To those suggesting that How do I include a JavaScript file in another JavaScript file? is a duplicate, I really don't see how they're related but that could be my inexperience in webdev. That question focuses on loading javascript inside other javascript files. This question is about loading a single javascript file into html. What makes this a duplicate?

Unmitigated

You should set the script type to module and import the function from index.js.

<script type="module">
import { myJavascriptFunc } from './index.js';
myJavascriptFunc();
</script>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Beginner HTML/CSS Mistake

HTML, javascript and jQuery - Beginner

Writing a function using switch case statement beginner javascript

Beginner mistake regarding memory allocation

Beginner javascript question: arrow function as optional argument

javaScript, function, priting my name (beginner)

Javascript beginner - function modifiing global const

Where to find some real programming assignments for a beginner webdev?

Print a value in php file using html [beginner]

Beginner at HTML5 Canvas, using layers?

Beginner mistake in Linq Extension Methods with Entity Framework

Mistake in code but I cant find it javascript html css

Using an HTML button to call a JavaScript function

Interacting with HTML audio object using JavaScript function

Getting html control value using javascript function

Editing cells of a table in html using Javascript function

How to get an HTML value using a JavaScript function

Using "time ago" Javascript function within HTML

Prepending text to html elements using a Javascript function

Beginner in JavaScript

Excel small mistake in function

Beginner confusion on JavaScript syntax regarding nested object function

JavaScript: TypeError: work.calls is not iterable in function decorator (beginner question)

Getting UNDEFINED at the end of a simple for loop function BEGINNER JAVASCRIPT

Haskell - Coding 'maximum' without actually using the function [beginner]

Beginner: Using a void function to calculate carpet cost in Python?

Simple Dimple graph not showing :( beginner mistake most likely

How to implement undo function for HTML canvas using JavaScript?

How to call a function by pressing enter key in html using javascript?