Java Script
Back Next

Client-Side JavaScript


Client-side JavaScript is the version of JavaScript that runs directly in a user’s web browser. It allows websites to be interactive, responsive, and dynamic without needing to reload the page or communicate with the server for every action.

In short: Client-side = runs on the user’s computer (browser).


1️⃣ How Client-Side JavaScript Works

  1. Browser loads the HTML and CSS for the webpage.
  2. JavaScript is executed inside the browser.
  3. JS can change HTML elements, CSS styles, and respond to user actions.
  4. All changes are visible immediately to the user.

Flow Diagram (simplified):

User Action → Browser → Client-Side JavaScript → Updates Page Dynamically


2️⃣ Advantages of Client-Side JavaScript


3️⃣ Limitations of Client-Side JavaScript


4️⃣ Where Client-Side JS is Used

  1. Form Validation – Check inputs before submission.
  2. Interactive Buttons – Show/hide content on click.
  3. Dynamic Content – Change text, images, or styles without reloading.
  4. Animations & Effects – Slide menus, image sliders, hover effects.
  5. Games – Browser-based mini-games (tic-tac-toe, counters).
  6. Fetching Data – Use AJAX or Fetch API to update parts of a page.


Compilation of Entire Code 

Explanation:

  • updateGreeting() → Prompts user to enter a name and updates the paragraph text dynamically.
  • changeColor() → Picks a random color from an array and changes the paragraph color immediately.
  • All of this happens on the client side, no server requests are needed.


Example Code:
<!DOCTYPE html>
<html>
<head>
  <title>Client-Side JavaScript Example</title>
  <style>
    body { font-family: Arial; padding: 20px; }
    button { padding: 10px 20px; margin: 5px; cursor: pointer; }
    p { font-size: 18px; color: darkblue; }
  </style>
</head>
<body>

  <h1>Client-Side JavaScript Demo</h1>

  <p id="greeting">Hello, User!</p>
  <button onclick="updateGreeting()">Click Me</button>

  <p id="colorText">Change my color!</p>
  <button onclick="changeColor()">Change Color</button>

  <script>
    function updateGreeting() {
      let name = prompt("Enter your name:");
      if(name) {
        document.getElementById("greeting").innerHTML = "Hello, " + name + "!";
      } else {
        document.getElementById("greeting").innerHTML = "Hello, Guest!";
      }
    }

    function changeColor() {
      let colors = ["red", "green", "blue", "orange", "purple"];
      let randomColor = colors[Math.floor(Math.random() * colors.length)];
      document.getElementById("colorText").style.color = randomColor;
    }
  </script>

</body>
</html>
Java Script
Introduction Application of JavaScript Defining JavaScript Client-Side JavaScript JavaScript Syntax JavaScript Varaibles JavaScript Operators JavaScript Statements JavaScript Arrays Sorting JavaScript Array JavaScript Functions JavaScript Objects JavaScript Switch Case & While Loops JavaScript For & For-In Loops JavaScript Cookies JavaScript Form Validation JavaScript Error and Exception Handling JavaScript Animation
All Courses
Bootstrap Content Writing CSS Cyber Security Data Analysis Deep Learning Email Marketing Excel HTML Java Script Machine Learning MySQLi PHP Power Bi Python for Analysis SEO SMM SQL