Home > Back-end >  Linking javascript file to html file does not work
Linking javascript file to html file does not work

Time:01-31

I am starting to learn JavaScript at the moment. In a course on YouTube the Tutor gave an example for linking a .js file to a .html file. Unfortunately it is not working. Can anyone tell me why?

index.html File:

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
<h1>Mello</h1>
<script> "sandbox.js" </script>


</body>
</html>

sandbox.js File:

alert("HELLO");

CodePudding user response:

You can include the javascript file in HTML as:

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

CodePudding user response:

or you can write code in script like this

<script>
  alert("HELLO");
</script>

CodePudding user response:

The js file path needs to be passed as an attribute src of script

eg.

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

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/script

  •  Tags:  
  • Related