About Lesson
Internal JavaScript (Using <script> Tag in HTML):
In this method, JavaScript is written within the HTML document but inside the <script> tag. This is useful when the script is only relevant to the specific page. You can place the <script> tag either in the <head> or at the end of the <body> section.
Example:
JavaScript
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Internal JavaScript Example</title>
<script>
function showMessage() {
alert('Hello from Internal JavaScript!');
}
</script>
</head>
<body>
<button onclick="showMessage()">Click Me</button>
</body>
</html>
- Head Section: Placing the script inside the <head> section can cause the script to run before the page content is fully loaded.
- End of Body: It’s common to place the <script> tag just before the closing </body> tag. This ensures that the HTML content loads first, and then the script executes.