HTML DOM Objects
Understanding the Document Object Model in JavaScript
🌐 What is the DOM?
The DOM (Document Object Model) is a programming interface that represents HTML documents as objects. JavaScript can access and modify these objects to change the webpage dynamically.
// Access the document object
console.log(document.title); // Page title
console.log(document.URL); // Current URL
document.body.style.backgroundColor = "lightblue";
Output:
HTML DOM Objects - Complete JavaScript Tutorial | Codorb
https://example.com/html-dom-objects.html
Background color changes to light blue
Main DOM Objects
Document
Represents the entire HTML document
document.title
document.body
document.head
Window
Represents the browser window
window.alert("Hello!")
window.location.href
window.innerWidth
Navigator
Information about the browser
navigator.userAgent
navigator.language
navigator.platform
Location
Information about current URL
location.href
location.hostname
location.pathname
🔹 Document Object Properties
The document object provides access to the HTML document:
// Basic document properties
console.log(document.title); // Page title
console.log(document.URL); // Full URL
console.log(document.domain); // Domain name
console.log(document.lastModified); // Last modified date
console.log(document.readyState); // Loading state
// Document structure
console.log(document.doctype); // DOCTYPE declaration
console.log(document.documentElement); // element
console.log(document.head); //
element
console.log(document.body); // element
// Document content
console.log(document.forms.length); // Number of forms
console.log(document.images.length); // Number of images
console.log(document.links.length); // Number of links
Output:
HTML DOM Objects - Complete JavaScript Tutorial | Codorb
https://example.com/html-dom-objects.html
example.com
12/15/2023 10:30:45
complete
[object DocumentType]
[object HTMLHtmlElement]
[object HTMLHeadElement]
[object HTMLBodyElement]
0
2
5
🔹 Window Object Properties
The window object represents the browser window:
// Window dimensions
console.log(window.innerWidth); // Viewport width
console.log(window.innerHeight); // Viewport height
console.log(window.outerWidth); // Browser window width
console.log(window.outerHeight); // Browser window height
// Screen information
console.log(window.screen.width); // Screen width
console.log(window.screen.height); // Screen height
// Window methods
window.alert("Hello World!"); // Show alert dialog
window.confirm("Are you sure?"); // Show confirmation dialog
window.prompt("Enter your name:"); // Show input dialog
// Window navigation
window.open("https://example.com"); // Open new window
window.close(); // Close current window
window.print(); // Print page
Output:
1920
1080
1936
1096
1920
1080
Alert dialog appears
Confirmation dialog appears
Input dialog appears
🔹 Navigator Object
The navigator object contains information about the browser:
// Browser information
console.log(navigator.appName); // Browser name
console.log(navigator.appVersion); // Browser version
console.log(navigator.userAgent); // User agent string
console.log(navigator.platform); // Operating system
console.log(navigator.language); // Browser language
// Browser capabilities
console.log(navigator.cookieEnabled); // Cookies enabled?
console.log(navigator.onLine); // Online status
console.log(navigator.javaEnabled()); // Java enabled?
// Geolocation (if available)
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
console.log("Latitude: " + position.coords.latitude);
console.log("Longitude: " + position.coords.longitude);
});
}
Output:
Netscape
5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36
Win32
en-US
true
true
false
Latitude: 40.7128
Longitude: -74.0060
🔹 Location Object
The location object contains information about the current URL:
// URL components
console.log(location.href); // Full URL
console.log(location.protocol); // Protocol (http: or https:)
console.log(location.hostname); // Domain name
console.log(location.port); // Port number
console.log(location.pathname); // Path after domain
console.log(location.search); // Query string
console.log(location.hash); // Fragment identifier
// URL manipulation
location.assign("https://example.com"); // Navigate to URL
location.replace("https://example.com"); // Replace current page
location.reload(); // Reload page
// Example with a sample URL: https://example.com:8080/page.html?name=john#section1
// location.href → "https://example.com:8080/page.html?name=john#section1"
// location.protocol → "https:"
// location.hostname → "example.com"
// location.port → "8080"
// location.pathname → "/page.html"
// location.search → "?name=john"
// location.hash → "#section1"
Output:
https://example.com/html-dom-objects.html
https:
example.com
/html-dom-objects.html
Page navigates to new URL
🔹 History Object
The history object allows navigation through browser history:
// History navigation
console.log(history.length); // Number of pages in history
// Navigate through history
history.back(); // Go back one page
history.forward(); // Go forward one page
history.go(-2); // Go back 2 pages
history.go(1); // Go forward 1 page
// Modern history API (HTML5)
history.pushState({page: 1}, "Title", "/page1"); // Add to history
history.replaceState({page: 2}, "Title", "/page2"); // Replace current
// Listen for history changes
window.addEventListener('popstate', function(event) {
console.log('History changed:', event.state);
});
// Example usage
function navigateToPage(pageNumber) {
history.pushState({page: pageNumber}, `Page ${pageNumber}`, `/page${pageNumber}`);
// Update page content here
}
Output:
5
Browser navigates back
Browser navigates forward
URL changes without page reload