简体   繁体   中英

How to include a class file in another file in pure javascript

OK, the question seems to have already been asked, but it hasn't as is, some ask to include file in another, some ask to import functions, some accept jquery... I'm working only in pure javascript, my files (simplified but still doesn't work):

index.html

<!DOCTYPE HTML>
<html lang="fr">

<head>
    <meta content="text/html; charset=utf-8" http-equiv="Content-Type">
    <meta name="Content-Type" content="UTF-8">
    <script src='editable.js'></script>
    <script src='global.js'></script>

</head>

<body>

global.js

window.addEventListener('load',Init);

function Init()
{
    var test=new Editable();    //this throw a undefined Element error
}

editable.js

export class Editable
{
    constructor()
    {
        alert("hello");
    }
}

You can "include" it directly from HTML, such as:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM