Mastering the Browser Object Model: Essential Objects and Methods for Web Development

Shravan Meena
4 min readApr 8, 2023

--

Introduction

The Browser Object Model (BOM) is a JavaScript API that provides access to the browser’s window object and other browser-specific objects and properties. These objects and properties are not part of the JavaScript language, but are instead provided by the browser environment. In this document, we will explore the BOM and some of its key objects and methods.

The Window Object

The window object is the top-level object in the BOM and represents the browser window itself. It contains a number of properties and methods that allow you to interact with the window and its contents. Here are some commonly used properties and methods of the window object:

  • window.alert(): Displays a simple dialog box with a message and an OK button.
  • window.prompt(): Displays a dialog box with a message and a text input field, and returns the value entered by the user.
  • window.confirm(): Displays a dialog box with a message and OK and Cancel buttons, and returns true if the user clicks OK and false if the user clicks Cancel.
  • window.location: Contains information about the current URL, including the protocol, host, path, and query string.
  • window.document: Represents the DOM (Document Object Model) of the current page, and provides access to the HTML elements and their properties and methods.
  • window.innerWidth and window.innerHeight: Return the width and height of the browser window, excluding the browser's interface elements such as toolbars and scrollbars.

The Navigator Object

The navigator object provides information about the browser itself, including the browser name, version, and platform. Here are some commonly used properties of the navigator object:

  • navigator.userAgent: Returns the user agent string, which contains information about the browser and operating system being used.
  • navigator.platform: Returns the platform (e.g. "Win32" or "MacIntel") on which the browser is running.
  • navigator.language: Returns the preferred language of the user, based on the browser settings.

The Screen Object

The screen object provides information about the user’s screen, such as its size and resolution. Here are some commonly used properties of the screen object:

  • screen.width: Returns the width of the user's screen in pixels.
  • screen.height: Returns the height of the user's screen in pixels.
  • screen.availWidth: Returns the width of the user's screen minus interface features like the Windows taskbar.
  • screen.availHeight: Returns the height of the user's screen minus interface features like the Windows taskbar.

The History Object

The history object provides access to the user’s browsing history. Here are some commonly used methods of the history object:

  • history.back(): Moves the user back one page in the browsing history.
  • history.forward(): Moves the user forward one page in the browsing history.
  • history.go(n): Moves the user to a specific page in the browsing history, where n is a positive or negative integer indicating the number of pages to move.

The Location Object

The location object represents the current URL of the browser window and provides methods for working with the URL. Here are some commonly used properties and methods of the location object:

  • location.href: Returns the complete URL of the current page.
  • location.protocol: Returns the protocol (e.g. "http:" or "https:") of the current page.
  • location.hostname: Returns the hostname (e.g. "example.com") of the current page.
  • location.pathname: Returns the path (e.g. "/path/to/page.html") of the current page.
  • location.search: Returns the query string (e.g. "?param1=value1&param2=value2") of the current page.
  • location.reload(): Reloads the current page.
  • location.assign(url): Loads a new page at the specified URL.

The Document Object

The document object represents the HTML document currently displayed in the browser window and provides methods for working with the document. Here are some commonly used properties and methods of the document object:

  • document.getElementById(id): Returns the element with the specified ID.
  • document.getElementsByClassName(className): Returns an array of elements with the specified class name.
  • document.getElementsByTagName(tagName): Returns an array of elements with the specified tag name.
  • document.createElement(tagName): Creates a new element with the specified tag name.
  • document.createTextNode(text): Creates a new text node with the specified text.
  • document.write(html): Writes HTML code to the document, replacing the current content.

The Event Object

The event object represents an event that occurs in the browser, such as a mouse click or key press. Here are some commonly used properties and methods of the event object:

  • event.target: Returns the element that triggered the event.
  • event.preventDefault(): Prevents the default action associated with the event.
  • event.stopPropagation(): Stops the event from propagating to parent elements.

The Window Timers

The window object provides several timers that allow you to schedule code to run at a later time. Here are some commonly used timers:

  • setTimeout(function, delay): Executes a function after a specified delay in milliseconds.
  • setInterval(function, interval): Executes a function repeatedly at a specified interval in milliseconds.
  • clearTimeout(timeoutID): Cancels a setTimeout timer.
  • clearInterval(intervalID): Cancels a setInterval timer.

The Storage Objects

The storage objects provide a way to store data in the browser, either temporarily or permanently. Here are the two types of storage objects:

  • localStorage: Stores data permanently, until it is deleted by the user or cleared by the browser.
  • sessionStorage: Stores data temporarily, until the browser window or tab is closed.

Both types of storage objects have the same methods and properties, which allow you to store, retrieve, and remove data.

Conclusion

The Browser Object Model is a powerful API that provides access to a wealth of browser-specific information and functionality. In this document, we have covered some of the most commonly used objects and methods of the BOM, including the window object, navigator object, screen object, and history object. By mastering these objects and methods, you can create more dynamic and interactive web pages that provide a better user experience.

--

--

Shravan Meena
Shravan Meena

Written by Shravan Meena

Writing code is my passion. I firmly believe in the transformative and enhancing power of programming, and how it can improve the lives of those around world.

No responses yet