Web Unit 5
Web Unit 5
1. Introduction
JavaScript is one of the most widely used scripting languages for client-side web development. It
allows developers to create dynamic and interactive web pages. While learning syntax is important,
understanding how to write practical, efficient, and maintainable scripts is crucial for success in
real-world applications.
This topic focuses on practical programming tips that help developers write better JavaScript code.
These tips improve readability, maintainability, performance, and debugging capabilities of
scripts.
Good Practice:
Bad Practice:
let x = 25;
let y = "Soap";
• Use camelCase for variables (e.g., userAge, firstName)
• Avoid single-letter or unclear names unless used in short scopes (e.g., loop counters)
greetUser("Sangeetha");
greetUser("Priya");
<script>
function showInfo() {
let width = window.innerWidth;
let height = window.innerHeight;
alert("Window Size: " + width + " x " + height);
}
</script>
</body>
</html>
This code shows the current size of the browser window when the button is clicked.
1. Introduction
The Document Object in JavaScript is a part of the Window Object and represents the entire HTML
or XML document that is displayed in the browser. Through the Document Object Model (DOM),
JavaScript can access and manipulate the content, structure, and style of the web document.
The document object is essentially the root of the DOM tree and is the gateway to all HTML elements
on the page. It allows dynamic interaction such as reading content, changing element attributes,
adding new elements, or applying styles.
Example:
console.log(document.title); // Returns the title of the web page
3. DOM Implementation
The Document Object Model (DOM) is a programming interface for HTML and XML documents. It
represents the page so that programs can change the document structure, style, and content.
Key Concepts:
• The DOM represents a document as a tree structure.
• Each element is called a "node."
• JavaScript interacts with the DOM to access or modify elements.
DOM Tree Example:
<html>
<body>
<p>Hello World</p>
</body>
</html>
DOM Tree:
Document
└── html
└── body
└── p
└── "Hello World"
</body>
</html>
This script changes the content and background of a paragraph when the button is clicked.
Introduction
The Browser Object in JavaScript represents the browser window and provides methods and
properties to interact with the browser itself. It is part of the Window object, which represents the
entire browser window in a web page. Through the browser object, developers can manipulate
browser settings, manage user interactions, control the history, and much more.
Introduction
The Form Object in JavaScript is used to access and manipulate the HTML forms on a webpage.
Forms are a key component in web development, allowing users to input data that can be sent to a
server for processing. The Form Object provides methods and properties that allow developers to
interact with form elements, validate input, and submit forms dynamically.
Introduction
The Navigator Object in JavaScript provides information about the browser and the system it is
running on. This object is essential for feature detection and user agent analysis, allowing developers
to adapt their applications based on the browser, platform, or device capabilities. It helps in
identifying the browser's version, operating system, and other important details, which can be used for
optimizing or tailoring user experiences.
Introduction
The Screen Object in JavaScript provides information about the user's screen. It allows developers to
access details such as the screen’s width, height, and resolution, which can be useful for adjusting
content to fit various screen sizes. This is especially important in responsive web design, where
content needs to adapt to different device screens. The screen object does not provide direct methods
for modifying the screen but provides valuable properties that can be used to enhance user experience.