Build: The Web

Adding JavaScript to HTML

Between the <script> and </script> elements in HTML, JavaScript code is placed. An HTML document can contain any number of scripts. Scripts can be placed in the <body>, the <head>, or both sections of an HTML page.
<head>  <script>    alert("Hello");  </script> </head>
A JavaScript function is placed in the <head> section of an HTML page in this example.
<body>  <!-- your content goes here -->  <script>    alert("Hello");  </script> </body>
A JavaScript function is placed in the <body> section of an HTML page in this example.
alert("Hello");
<script src="main.js"></script>
Create a .js script file, such as main.js, and place it in the head or body of the html document.
badge