Introduction to Client-Side Scripting

๐Ÿ“˜ Client-Side Scripting – Script Placed Inside Body Section


๐Ÿ”น 3.11 Introduction to Client-Side Scripting

๐ŸŒ What is Client-Side Scripting?

  • A client-side script runs on the user's web browser, not on the server.

  • Used to create interactive and dynamic web pages.

  • Examples: JavaScript, VBScript (JavaScript is most common).


๐Ÿ› ️ Why Use Client-Side Scripting?

  • Enhances user experience by making pages responsive and interactive.

  • Can perform tasks like:

    • Validating forms before submission

    • Displaying alert messages

    • Changing web content dynamically

    • Performing calculations without reloading the page


⚙️ JavaScript – The Most Popular Client-Side Language

  • Created to “make webpages alive”.

  • No compilation needed – written directly in HTML files.

  • Supported by all modern browsers (Chrome, Firefox, Edge, etc.).

  • Runs as soon as the page is loaded in the browser.


๐Ÿงฑ Limitations of HTML Alone

HTML is only a markup language, which:

  • Structures content using elements like <p>, <h1>, <img>, etc.

  • Cannot perform logic like:

    • Addition or subtraction

    • Conditional checks (if-else)

    • Loops (for, while)

    • User interactions


Why Embed JavaScript in HTML?

By embedding JavaScript:

  • You can add logic and interaction directly inside the web page.

  • For example:

    <script>
      alert("Welcome to our website!");
    </script>
    

๐Ÿ“Where is JavaScript Placed in HTML?

  • Typically inside the <script> tag.

  • Can be placed:

    • Inside the <head> section

    • Inside the <body> section (often preferred for interaction)

๐Ÿงพ Example: Script Inside Body

<!DOCTYPE html>
<html>
<head>
  <title>Simple JavaScript</title>
</head>
<body>
  <h1>Welcome</h1>

  <script>
    let x = 10;
    let y = 20;
    let sum = x + y;
    document.write("Sum of x and y is: " + sum);
  </script>

</body>
</html>

๐Ÿ’ก Key Benefits for Students

  • Helps understand basic programming logic.

  • Prepares for advanced subjects like Web Development, Computer Applications, or eCommerce systems.

  • JavaScript is beginner-friendly and widely used in jobs.


๐Ÿ“Œ Summary Points

Feature HTML JavaScript
Language Type Markup Scripting
Use Structure Logic/Interactivity
Executes Static Dynamic
Examples <p>, <h1> if, for, alert()
Browser Execution No Yes

๐Ÿ“˜ 3.12 Scripting Language 

✅ What is a Script?

  • A script is a sequence of commands executed by a scripting engine.

  • Primarily used to make web pages dynamic (i.e., interactive and responsive).

  • Scripts are typically written in plain text and can be edited using a text editor.


๐Ÿ–ฅ️ Scripting Languages in Web Development

  • Used to automate tasks on web pages.

  • Can be client-side (executed in browser) or server-side (executed on server).


⚙️ JavaScript: A Popular Scripting Language

JavaScript is widely used for client-side scripting and is supported by all modern browsers.

๐Ÿ”ง Inserting JavaScript into HTML:

JavaScript code is placed between <script> and </script> tags.

<script type="text/javascript">
  // JavaScript code goes here
</script>

๐Ÿ”ธ The type="text/javascript" attribute specifies the scripting language (though it's optional in modern HTML5).


๐Ÿง  Where to Place JavaScript in HTML?

1. Inside the <body> Section:

<!DOCTYPE html>
<html>
  <head><title>First</title></head>
  <body>
    <script type="text/javascript">
      // JavaScript statements
    </script>
  </body>
</html>

2. Inside the <head> Section:

<!DOCTYPE html>
<html>
  <head>
    <title>Second</title>
    <script type="text/javascript">
      // JavaScript statements
    </script>
  </head>
  <body></body>
</html>

โ„น️ JavaScript can also be placed in both <head> and <body> sections if needed.


๐Ÿ“Œ Best Practices:

  • Terminate each JavaScript statement with a semicolon (;) – helps avoid errors, especially in complex scripts.

  • ❗ Semicolon is not strictly required if each statement is on a new line, but it's recommended for clarity and compatibility.


๐Ÿ“ Summary:

Concept Description
Script Series of commands run by a scripting engine
Scripting Language Used for dynamic web content
JavaScript Popular client-side scripting language
<script> tag Used to include JavaScript in HTML
Placement in HTML Inside <head>, <body>, or both
Semicolon (;) Recommended to end JavaScript statements

3.13 Variables in JavaScript

๐Ÿ”น What is a Variable?

  • A variable is a basic unit of storage used to store data in a JavaScript program.

  • It holds values that can change during the execution of the program.


๐Ÿ”น Rules for Declaring Variables

A variable name in JavaScript must follow these rules:

  1. Must begin with a letter (a-z or A-Z), underscore _, or dollar sign $.

  2. ✅ Can include letters, digits (0-9), underscores (_), and dollar signs ($).

  3. Cannot start with a digit.

  4. Case-sensitive: sumSUM.

  5. Cannot contain spaces or special characters (e.g., @, #, !, &, etc.), except _ and $.

  6. Cannot use reserved JavaScript keywords like document, while, if, var, etc.

  7. Maximum length: Up to 255 characters.


๐Ÿ”น Declaring Variables

Variables are declared using the var keyword (older way, still valid in many cases).

Syntax:

var variablename; // declaration
var a, b, c; // multiple variables
var x = 10, y = 20; // with initialization

Examples:

var a;        // declaring a variable
var x, y, z;  // multiple variable declarations
var p = 5, q = 10;  // declared and initialized

3.14 Data Types in JavaScript (Overview)

JavaScript supports different data types to store different types of values:

Data Type Description Example
Number Numeric values var age = 25;
String Text or characters var name = "John";
Boolean Logical values: true or false var isOpen = true;
Undefined Variable declared but not initialized var x;
Null Intentional absence of any value var z = null;
Object Collection of key-value pairs var person = {name: "Alice", age: 22};
Array Ordered collection of values var nums = [1, 2, 3];

Do It Yourself

๐Ÿ”ธ Define five correct variable names:

  1. userName

  2. _score

  3. $amount

  4. totalMarks

  5. age_42

๐Ÿ”ธ Define five incorrect variable names:

  1. 123abc ❌ (starts with number)

  2. first name ❌ (contains space)

  3. user-name ❌ (contains hyphen)

  4. @value ❌ (special character not allowed)

  5. while ❌ (JavaScript keyword)


๐Ÿ”ธ List Some Scripting Languages:

  • JavaScript

  • Python

  • PHP

  • Ruby

  • Perl

  • VBScript

  • Bash



Comments

Popular posts from this blog