Document Object and Window Object

Understanding the Document Object Model (DOM) is paramount. The Document Object and Window Object are fundamental components of the DOM, playing crucial roles in how web pages are structured, accessed, and manipulated through JavaScript. In this documentation blog, we'll delve into the specifics of these objects, exploring their properties, methods, and practical examples to solidify your understanding.

Introduction to Document Object and Window Object

Document Object: The Document Object represents the HTML document loaded in the browser window. It serves as an entry point to the content of the web page and provides methods and properties for accessing and manipulating its elements. Every HTML element within the document is part of the Document Object.

Window Object: The Window Object represents the browser window that contains the DOM. It provides access to the browser's functionalities and properties related to the window, such as navigation, resizing, and timing. The Window Object acts as a global object for JavaScript in the browser environment.

Properties and Methods

Document Object:

  • document.getElementById(id): Retrieves an element by its ID.

  • document.getElementsByClassName(className): Retrieves elements by their class name.

  • document.getElementsByTagName(tagName): Retrieves elements by their tag name.

  • document.querySelector(selector): Retrieves the first element that matches the specified CSS selector.

  • document.createElement(tagName): Creates a new HTML element.

  • document.title: Gets or sets the title of the document.

  • document.body: Gets the body element of the document.

Window Object:

  • window.alert(message): Displays an alert dialog with the specified message.

  • window.open(url, name, specs, replace): Opens a new browser window or a new tab with the specified URL.

  • window.close(): Closes the current window.

  • window.innerWidth and window.innerHeight: Gets the inner width and height of the browser window.

  • window.location: Provides information about the current URL and can be used for navigation.

  • window.setTimeout(function, milliseconds): Executes a function after a specified number of milliseconds.

Example Usages

Document Object:

// Accessing an element by its ID and changing its content
let element = document.getElementById("example");
element.innerHTML = "New Content";

// Creating a new paragraph element and appending it to the body
let paragraph = document.createElement("p");
paragraph.textContent = "This is a new paragraph.";
document.body.appendChild(paragraph);

Window Object:

// Displaying an alert message
window.alert("Welcome to our website!");

// Opening a new window
let newWindow = window.open("https://example.com", "_blank");

// Setting a timeout to execute a function after 2 seconds
window.setTimeout(() => {
    console.log("Timeout executed.");
}, 2000);

Conclusion

Understanding the Document Object and Window Object is fundamental for effective web development with JavaScript. These objects provide powerful tools for interacting with the DOM and browser environment, enabling developers to create dynamic and responsive web applications. By mastering their properties and methods, you gain the ability to manipulate web page content, handle user interactions, and control the browser window effectively.

Last updated