Including JavaScript in HTML


  In HTML, you can include JavaScript in several ways: inline, on the same page with the <script> tag, or through a linked external file.


Syntax:


  You can use JavaScript in your HTML document in the following ways:

  • Inline JavaScript: <button onclick="alert('Hello!')">.

  • Internal JavaScript: Inside the <script> tag in the same HTML file.

  • IExternal JavaScript: Using <script src="file.js">.

Purpose:


  Using JavaScript allows you to add interactivity and dynamism to your web pages.



Exercises



Interactive quiz 1: Drag and Drop.

Arrastra en el orden correspondiente.


Arrastra las opciones:

</script>
<script src="script.js">
document.write("Hello from JS");
<html>
</html>

Completa el código:

______
______
______
______
______

Interactive quiz 2: Fill in the blanks

Rellena los huecos en cada casilla.

<html>
  <head>
    <title>JavaScript Example</title>
  </head>
  <body>
    <h1>Print Hello from JavaScript</h1>
    <script>
      
    </script>
  </body>
</html>

Exercise 3:

Complete the code in the editor to display the message "Hello world from JS!" on the page.

* Write the code below. Correct characters will be shown in green and incorrect ones in red.

<html> <body> <p id="demo"></p> <script> document.getElementById("demo").innerHTML = "This text was added with JavaScript!"; </script> </body> </html>

How can you include JavaScript in an HTML file?



JavaScript Concepts and Reference

How can I include JavaScript in HTML? How to include a JavaScript function in HTML? How to enable JavaScript in HTML? How to connect HTML and JavaScript? JavaScript in HTML examples HTML with external JavaScript in an HTML page JavaScript code can only appear: Insert JavaScript function in HTML JavaScript link in HTML Add HTML code with JavaScript Call a JS in HTML Call JavaScript function from HTML