HTML nie wczytuje pliku .js

0

Witam,

Proszę o informację jaka jest przyczyna, iż html nie zaczytuje pliku js.
W przeglądarce pojawily się następujące błędy.

Uncaught SyntaxError: Cannot use import statement outside a module
index.html:8 Uncaught ReferenceError: firstCheck is not defined
at HTMLInputElement.onchange (index.html:8)
index.html:10 Uncaught ReferenceError: secondCheck is not defined
at HTMLInputElement.onchange (index.html:10)

<html>
<head>
  <title>Calculator</title>

</head>
<body>
  <div>
   <input onchange="firstCheck()" type="text" id="firstNumber" style="font-size: 22px;"/>
   <label id="mark"> &nbsp;</label>
   <input onchange="secondCheck()" type="text" id="secondNumber" />
   <label id="equal">=</label>
   <label id="score"></label>
 </div>
 <div style="margin-top:10px;">
   <button onclick="addition" id="addition">+</button>
   <button onclick="subtraction" id="subtraction">-</button>
   <button id="multiplication">*</button>
   <button id="division">/</button>
   <button id="exponentiation">x^</button>
 </div>
  <p id="notification"></p>
<script type="text/Javascript" src="index.js"></script>
</body>
</html>

JAVASCRIPT

// Import stylesheets
import './style.css';

// Write Javascript code!

var score = document.getElementById("score");
var mark = document.getElementById("mark");
var notification = document.getElementById("notification");

var firstNumber = document.getElementById("firstNumber");
var secondNumber = document.getElementById("secondNumber");

function firstCheck(){
  if(isNaN(parseInt(firstNumber.value))){
    notification.textContent = "Only numbers !!";
    this.value = "";
  }else{
    notification.textContent = "";
  }
}

function secondCheck(){
  if(isNaN(parseInt(secondNumber.value))){
    notification.textContent = "Only numbers !!";
    this.value = "";
  }else{
    notification.textContent = "";
  }
}

function addition(){
let first = parseInt(firstNumber.value);
let second = parseInt(secondNumber.value);
  if(!isNaN(first) && !isNaN(second)){
    score.textContent = first + second;
    mark.textContent = "+";
  }else{
    notification.textContent = "Please put some numbers in the given fields!";
  }
}

function subtraction(){
  let first = parseInt(firstNumber.value);
  let second = parseInt(secondNumber.value);
    if(!isNaN(first) && !isNaN(second)){
    let result = first - second;
      score.textContent = result;
      mark.textContent = "-";
    }else{
      notification.textContent = "Please put some numbers in the given fields!";
    }
}

function exponentiation(){
  let first = parseInt(firstNumber.value);
  let second = parseInt(secondNumber.value);
  if(!isNaN(first) && !isNaN(second)){
    let result = first;
    if(second === 0){
      result = 1;
    }else{
      for(let i = 1; i < second;i++){
        result = result * first;
      }
      score.textContent = result;
      mark.textContent = "^";
  }else{
    notification.textContent = "Please put some numbers in the given fields!";
  }
}
2

Twój kod powinien odpalić po zamianie tagu script i dodaniu type="module"

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

Starsze przeglądarki słabo sobie radzą z takim zapisem i często nie jest to wspierane, więc zazwyczaj kompiluje się kod JavaScript do ES5. Żeby to zrobić musisz skorzystać z narzędzi webpack i babel.

1 użytkowników online, w tym zalogowanych: 0, gości: 1