Import functions from another js file. Javascript

The following works for me in Firefox and Chrome. In Firefox it even works from file:///

models/course.js

export function Course() {
    this.id = '';
    this.name="";
};

models/student.js

import { Course } from './course.js';

export function Student() {
    this.firstName="";
    this.lastName="";
    this.course = new Course();
};

index.html

<div id="myDiv">
</div>
<script type="module">
    import { Student } from "https://stackoverflow.com/questions/48211891/./models/student.js";

    window.onload = function () {
        var x = new Student();
        x.course.id = 1;
        document.getElementById('myDiv').innerHTML = x.course.id;
    }
</script>

Leave a Comment