JavaScript in the Browser
Learn how JavaScript works inside a web browser and how it can interact with web pages and browser features.
JavaScript Runs in the Browser
When you open a web page, the browser can load and execute JavaScript code.
JavaScript can respond to user actions, change page content, read form values, and use browser features.
JavaScript can work with browser objects such as Window, Document, and Navigator.
Important Browser Objects
Three useful browser objects are window, document, and navigator.
Window
Represents the browser window or tab where the web page is running.
Document
Represents the current web page and provides access to the DOM.
Navigator
Provides information about the browser environment.
The DOM represents the structure of an HTML page as a tree of objects.
How JavaScript Interacts with the Browser
JavaScript and the DOM
DOM means Document Object Model. It represents the HTML page as a tree of objects.
JavaScript can use the DOM to find HTML elements and change their content.
Simple Example
JavaScript can select a heading and change its text without reloading the entire page.
DOM Example
const heading = document.querySelector("h1");
heading.textContent = "Welcome to CIIT Training Institute!";
Browser API Example
JavaScript can also read information provided by browser APIs.
console.log("Welcome to CIIT Training Institute!");
console.log("JavaScript is running in the browser.");
console.log("Browser Language:", navigator.language);
Example Output
JavaScript is running in the browser.
Browser Language: en-US
Common Web APIs
Browsers provide Web APIs that JavaScript can use for different browser-related tasks.
Examples
• DOM API
• Web Storage API
• Fetch API
• Timer APIs
• Web Workers API
CIIT Training Institute Example
JavaScript can be used to create interactive learning websites such as the CIIT Training Institute tutorial platform.
const institute = "CIIT Training Institute";
console.log("Welcome to " + institute);
console.log("Learn JavaScript Step by Step.");
Output
Learn JavaScript Step by Step.
Summary
JavaScript can run inside a web browser and interact with web pages and browser features. The Window, Document, and Navigator objects are important parts of the browser environment. JavaScript can also use Web APIs to build interactive web applications.