🚀JavaScript BOM (Browser Object Model): Your JavaScript Spaceship Dashboard
Welcome Aboard 🚀
Imagine this: you’re in a high-tech spaceship zooming through the internet galaxy. That spaceship is your web browser—Chrome, Firefox, Safari—you name it. 🛸
Inside that spaceship is a clever pilot: JavaScript. But even the smartest pilot needs a dashboard to control things like opening new doors, talking to passengers, checking the ship’s dimensions, and warping through history.
That dashboard? It’s called the BOM – Browser Object Model. And today, we’re going to explore its controls like curious space cadets!
🌌 So, What is the BOM?
The BOM is JavaScript’s way of interacting with the browser itself, not just the web page.
While the DOM is all about the webpage content (like paragraphs, buttons, and images), the BOM is about the browser environment.
With the BOM, JavaScript can:
- Open or close browser tabs/windows
- Display alerts, prompts, or confirmations
- Read the screen or browser dimensions
- Peek into the browser’s identity and settings
- Travel through browsing history
- Set timers and intervals
🛠️ The BOM Toolkit: Console, Controls & Cool Tricks
Let’s take a look at the major buttons and switches JavaScript has access to on the spaceship control panel:
🪟 1. window
– The Captain’s Deck
At the heart of the BOM is the window
object. It’s like the main control room of your browser. Nearly everything in the BOM lives inside it.
Fun fact: even things like alert()
, prompt()
, and setTimeout()
are part of the window
object.
alert("Welcome aboard!"); // Same as:
window.alert("Welcome aboard!");
So whenever you see window.something
, it’s just JavaScript saying, “Hey browser, I need this control.”
🌐 2. window.open()
– Open a New Tab or Window
Want to open a new page from JavaScript? Here’s your warp gate opener:
window.open("https://agunechemba.github.io");
Just like that—WHOOSH!—a new tab flies open.
📐 3. window.innerWidth
& window.innerHeight
– Size Up the Viewport
Ever want to check the size of your browser window’s content area? These tools are your sensors:
console.log(window.innerWidth);
console.log(window.innerHeight);
Perfect when designing responsive websites or adjusting elements based on screen size!
🧭 4. navigator
– The Browser’s Passport & ID Card
The navigator
object holds info about the browser and device. It’s like a passport JavaScript can inspect.
console.log(navigator.userAgent); // Browser identity
console.log(navigator.language); // Language like "en-US"
Great for localization or browser-specific behavior!
🖥️ 5. screen
– Monitor Radar System
Need to know the actual screen size (not just the browser window)? Tap into the screen
object:
console.log(screen.width);
console.log(screen.height);
Useful for centering popups or full-screen apps!
⏱️ 6. setTimeout()
& setInterval()
– JavaScript’s Time Machines
Want to delay code execution or run something repeatedly? These time tools have you covered.
Wait, then run:
setTimeout(function() {
alert("3 seconds have passed!");
}, 3000);
Run forever (until stopped):
setInterval(function() {
console.log("Tick... Tock...");
}, 1000);
It’s like JavaScript setting an alarm or heartbeat function.
🧠 7. history
– The Browser Time Travel Button
Missed a page? Want to go back to the past or zoom into the future? The history
object lets you do just that:
history.back(); // Go back one step
history.forward(); // Go forward one step
Your browser history becomes a time machine JavaScript can control. 🕰️
🧠 TL;DR – BOM in a Nutshell
Feature | What it Does |
---|---|
window |
Main access point to all BOM stuff |
window.open() |
Opens a new tab or popup |
innerWidth , innerHeight |
Get the browser’s viewport size |
navigator |
Info about the browser and language |
screen |
Info about the user’s monitor |
setTimeout , setInterval |
Run code after delay or repeatedly |
history |
Navigate browser history |
📝 Review & Practice Time!
- What’s the main difference between the DOM and the BOM?
- How would you write a script to open a new browser tab to
https://openai.com
? - Write a mini script that logs the user’s screen width and height.
- What’s the key difference between
setTimeout()
andsetInterval()
? Give an example of each. - You want to make JavaScript go one page back in browser history—what method would you use?