đ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?
