
260 views
How to add JavaScript to html
To add JavaScript code to an HTML document, you can use the <script>
tag. Here’s how you can do it:
- Open your HTML document in a text editor or an HTML editor.
- Decide where you want to insert the JavaScript code within the HTML document. Typically, it’s placed inside the
<head>
section or just before the closing</body>
tag. - To insert the JavaScript code inside the
<head>
section, add the following code:
HTML
<head>
<script>
// JavaScript code goes here
</script>
</head>
- To insert the JavaScript code just before the closing
</body>
tag, add the following code:
HTML
<body>
<!-- HTML content goes here -->
<script>
// JavaScript code goes here
</script>
</body>
- Replace the
// JavaScript code goes here
comment with your actual JavaScript code. - Save the HTML document.
- Open the HTML document in a web browser to see the JavaScript code in action.
You can also include external JavaScript files in your HTML document using the <script>
tag with the src
attribute. For example:
HTML
<script src="script.js"></script>
In this case, the JavaScript code is stored in a separate file called “script.js” in the same directory as the HTML file.