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
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
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.<!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>