Contents

Scripting and HTML

JavaScript is an essential part of modern web development, adding interactivity and dynamic behavior to HTML pages. The <script> element allows you to embed JavaScript code within your HTML documents. Additionally, HTML provides event attributes (like onclick, onload, onmouseover, etc.) to respond to user interactions. The <noscript> element provides a fallback message when JavaScript is disabled in the user’s browser.

Including JavaScript in HTML ()

The <script> tag is used to include JavaScript in an HTML document. It can be placed in the <head> or <body> sections, or both, depending on the use case. JavaScript code can be written directly within the tag or linked externally.

Embedding JavaScript Directly:

You can embed JavaScript directly within the <script> tag:

				
					<!DOCTYPE html>
<html>
<head>
  <title>JavaScript Example</title>
</head>
<body>
  <h1>Welcome to My Website</h1>
  <script>
    document.write("Hello, this is JavaScript!");
  </script>
</body>
</html>

				
			

In this example, JavaScript writes text directly to the web page.

Including an External JavaScript File:

You can also link to an external JavaScript file using the src attribute of the <script> tag:

				
					<!DOCTYPE html>
<html>
<head>
  <title>External JavaScript</title>
  <script src="script.js"></script>
</head>
<body>
  <h1>External JavaScript File Example</h1>
</body>
</html>


				
			
External File (script.js):
				
					document.write("Hello, this is from an external JavaScript file!");


				
			
Placement of the <script> Tag:
  • In the <head>: Scripts in the head are executed before the page is rendered, which may delay loading.
  • At the End of the <body>: Placing scripts at the end of the body ensures that the page content loads first, which is generally better for performance.
  • Using defer or async Attributes:
    • defer: Tells the browser to continue parsing the HTML while the script loads and executes after the document is fully parsed.
    • async: Tells the browser to execute the script as soon as it’s downloaded, without blocking the page rendering.

 

Example using defer:

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

				
			

Handling Events in HTML (onclick, onload, onmouseover, etc.)

Events in JavaScript are actions or occurrences that happen in the browser, such as clicking a button, loading a page, or hovering over an element. You can use HTML attributes to specify how to handle these events.

Common Event Attributes:
  • onclick: Triggered when an element is clicked.
  • onload: Triggered when the page or an image is fully loaded.
  • onmouseover: Triggered when the mouse pointer is moved over an element.
  • onmouseout: Triggered when the mouse pointer is moved out of an element.
  • onchange: Triggered when the value of an input element changes.
 
Example: Using onclick to Handle Button Clicks
				
					<!DOCTYPE html>
<html>
<head>
  <title>Event Handling Example</title>
</head>
<body>
  <button onclick="displayMessage()">Click Me!</button>
  <script>
    function displayMessage() {
      alert("Button was clicked!");
    }
  </script>
</body>
</html>

				
			

When the button is clicked, the displayMessage function is called, displaying an alert with the message “Button was clicked!”

Example: Using onload to Execute Code When the Page Loads
				
					<!DOCTYPE html>
<html>
<head>
  <title>onload Event Example</title>
  <script>
    function pageLoaded() {
      alert("Page has finished loading!");
    }
  </script>
</head>
<body onload="pageLoaded()">
  <h1>Welcome to My Website</h1>
</body>
</html>

				
			

In this example, an alert is displayed as soon as the page finishes loading.

Example: Changing Image on Mouse Hover (onmouseover and onmouseout)
				
					<!DOCTYPE html>
<html>
<head>
  <title>Image Hover Example</title>
</head>
<body>
  <img decoding="async" src="image1.jpg" alt="Image" id="myImage" 
       onmouseover="changeImage()" 
       onmouseout="resetImage()">

  <script>
    function changeImage() {
      document.getElementById('myImage').src = 'image2.jpg';
    }

    function resetImage() {
      document.getElementById('myImage').src = 'image1.jpg';
    }
  </script>
</body>
</html>

				
			

When the user hovers over the image, it changes to a different image, and when the mouse moves away, it reverts to the original image.

The Element

The <noscript> tag provides fallback content for users who have JavaScript disabled in their browser. It’s a good practice to include this element to ensure your site is usable even without JavaScript.

Basic Usage:
				
					<!DOCTYPE html>
<html>
<head>
  <title>Noscript Example</title>
</head>
<body>
  <script>
    document.write("JavaScript is enabled!");
  </script>

  <noscript>
    <p>JavaScript is disabled or not supported by your browser. Please enable JavaScript or use a different browser to view this content.</p>
  </noscript>
</body>
</html>

				
			

If JavaScript is disabled, the text inside <noscript> is displayed. Otherwise, the JavaScript message “JavaScript is enabled!” is shown.

Summary

Including JavaScript in HTML through the <script> element allows you to add interactivity and dynamic behavior to web pages. You can either embed JavaScript directly within the HTML or link to an external script file. HTML events like onclick, onload, and onmouseover are used to trigger JavaScript functions in response to user interactions. The <noscript> element provides an alternative message or content for users who have JavaScript disabled, ensuring that the website remains accessible. These scripting tools are vital for creating a rich, interactive user experience.