The Browser Object Model (BOM) is a collection of objects provided by the browser that allows JavaScript to communicate with and control the browser window.
Unlike the DOM, which deals with the HTML document, the BOM deals with the browser environment itself.
The BOM allows developers to:
- Open new browser windows
- Resize browser windows
- Get screen information
- Access browser history
- Obtain browser details
- Display popup messages
- Navigate to different URLs
BOM (Browser Object Model) is a collection of browser-provided objects that enables JavaScript to interact with the browser and its components.
It acts as a bridge between:
JavaScript <------> Browser
The window object is the root object of BOM.
It represents:
Browser Window
It is automatically created by the browser.
Example:
window.alert("Welcome");or simply
alert("Welcome");because every BOM object belongs to the window object.
The navigator object contains information about:
- Browser name
- Browser version
- Platform
- User Agent
Example:
console.log(navigator.appName);
console.log(navigator.platform);
console.log(navigator.userAgent);The screen object provides information about the user's display screen.
Properties:
screen.width
screen.height
screen.availWidth
screen.availHeightThe history object stores the browser history.
Methods:
history.back()
history.forward()
history.go()Example:
history.back();Moves to previous page.
The location object contains information about the current URL.
Example:
console.log(location.href);
console.log(location.hostname);
console.log(location.protocol); Window
_______________________
| | |
Navigator Screen History
|
Location
|
Document (DOM)
The Window object is the top-most object.
BOM is used to:
- Communicate with browser
- Get screen size
- Navigate pages
- Display alerts
- Open new windows
- Access browser information
- Control browser history
<!DOCTYPE html>
<html>
<head>
<title>BOM Example</title>
</head>
<body>
<p id="Wid"></p>
<script>
document.getElementById("Wid").innerHTML =
"The width of browser window is : "
+ screen.width;
document.getElementById("Wid").innerHTML +=
"<br>The height of browser window is : "
+ screen.height;
</script>
</body>
</html><!DOCTYPE html>Declares HTML5 document.
<html>Root element.
<head>Contains metadata.
<title>BOM Example</title>Sets browser tab title.
<body>Contains webpage content.
<p id="Wid"></p>Creates an empty paragraph with id:
Wid
The screen details will be displayed here.
document.getElementById("Wid")Selects the paragraph element.
screen.widthReturns:
Browser Screen Width
Example:
1920
screen.heightReturns:
Browser Screen Height
Example:
1080
innerHTMLDisplays the data inside the HTML element.
Suppose your monitor resolution is:
1920 × 1080
Output:
The width of browser window is : 1920
The height of browser window is : 1080
alert("Welcome to JavaScript");Output:
-----------------------
Welcome to JavaScript
OK
-----------------------
confirm("Are you sure?");Output:
--------------------
Are you sure?
OK Cancel
--------------------
Returns:
true -> OK
false -> Cancel
prompt("Enter Your Name");Output:
-------------------------
Enter Your Name
_____________
OK
-------------------------
Returns the value entered by the user.
window.open();window.close();window.print();navigator.appName
navigator.appVersion
navigator.platform
navigator.userAgent
navigator.languageExample:
console.log(navigator.platform);Output:
Win32
location.href
location.hostname
location.protocol
location.pathnameExample:
console.log(location.href);Output:
https://www.example.com/index.html
history.back();Moves:
Current Page
↓
Previous Page
history.forward();Moves:
Current Page
↓
Next Page
history.go(-1)Moves one page back.
| DOM | BOM |
|---|---|
| Document Object Model | Browser Object Model |
| Represents HTML document | Represents Browser |
| Manipulates HTML elements | Manipulates Browser |
| Root object is document | Root object is window |
| Changes webpage contents | Controls browser behavior |
Answer:
BOM stands for Browser Object Model.
It allows JavaScript to communicate with and control the browser.
Answer:
windowAnswer:
- Window
- Navigator
- Screen
- History
- Location
Answer:
screenExample:
screen.width
screen.heightAnswer:
historyAnswer:
locationAnswer:
DOM manipulates HTML documents.
BOM manipulates browser components.
Answer:
Displays a popup alert message.
Example:
alert("Hello");- BOM stands for Browser Object Model.
- Window is the root object.
- DOM is a part of BOM.
- screen object gives screen information.
- navigator object gives browser details.
- history object manages browser history.
- location object manages URL information.
- alert(), confirm(), and prompt() are window methods.
The Browser Object Model (BOM) is a browser-provided interface that enables JavaScript to interact with browser components such as the window, screen, navigator, history, and location objects. Using BOM, developers can obtain browser information, manage navigation, display dialogs, access screen properties, and build dynamic and interactive web applications efficiently.