The Browser Object Model
The collection of these objects is known as the Browser Object Model (BOM). The BOM is a tree-like hierarchy of objects, each with useful access properties and methods. Essentially, this object allows you to talk with your user's browser.
Functions include querying the the browser window size, the main heading of the page, a pointer to the last page in a user's history and more!
Elements of the Browser Object Model
The BOM's root is the
window object, which displays properties of the current open window or tab. It has several children, most of which we'll cover in this section.
Here is a brief description of each element on the tree.
- Contains all HTML elements of the current page.
- Any frames embedded within the current page.
- Contains a stack of sites visited in the current session.
- Stores URL of the current page.
- Stores information about the browser being used, and the OS used by the browser.
- Display properties of the user's computer or device.
document element has much to cover, it deserves its own section; we'll cover the other elements here, starting with the
The Window Object
window object represents the current open window on a browser.
- Returns True if window is closed.
- Set the text on the status bar.
- The inner height of the page (not including toolbar, other tabs, etc).
- The inner width (excludes toolbar, other tabs, etc.).
- The outer height of a window (including toolbar, other tabs, etc).
- The outer width of a window.
- Window's current URL.
- Name of the window.
- Used with frames to refer to the window that created a particular window or is one level up from the frame.
- Reference to the window that the current window came from.
- The number of pixels user has scrolled horizontally from the upper-left corner of document.
- The number of pixels the user has been scrolled vertically.
- Horizontal coordinate of the window relative to the screen.
- Vertical coordinate of the window relative to the screen.
- The top-most parent window.
Some properties such as
parent return a reference to another window object.
Additionally, you can access its child elements -
navigator. We'll cover this in the next lesson.
window contains a good number of methods. In fact, we have already looked at one of them -
alert(), which outputs an popup dialog message to the user.
Some of these methods require a parameters, so Google the methods if you want to see exactly how a method works.
- Opens a popup window. User must click OK to close.
- Prompts user for an input
- Opens new browser window with specified URL.
- Closes window.
- Opens print dialog.
Moving a window object
There are also functions you can use to move the window object.
- Move the window relative.
- Moves the window to a specific location.
- Change size of window relative to current size.
- Change size of window to specified size.
Other BOM elements history, location, navigator, screen
There are a handful of other Browser Object Models that we will briefly go over here.
window.screen object obtains information about the user's screen.
- Width and height of the available screen (excluding taskbars).
- Width and height of the available screen (with taskbars).
- Number representing the bit depth of the color palette on user's screen.
window.navigator object is used to query a number of elements about the visitor's browser and environment.
- Determine language of browser.
- Check name of browser.
- Returns geolocation object used to locate user's position.
- Determines if user is online.
- Which platform user is using (OS X, Windows, Linux).
- Determines whether cookies are enabled.
- Check if user's Java is enabled.
window.location object provides information about the current URL.
- Returns anchor part of a URL.
- Returns hostname and port number of URL.
- Returns path or nort number of URL.
- Returns hostname of a URL.
- Returns protocol, hostname and portname of a URL.
- Loads new document.
- Reload current document.
- Replace current document.
window.history gives you information regarding the user's history. Due to security concerns, we can't directly access which URL's our users have visited, but we may find other parameters.
- Return the length of the user's history.
- Move the user forward or back a page.
- Loads specific URL from history list.