Browser Object Model

Browser Object Model

Day 21: 60 Days Of Full Stack Web Development Challenge

  • The Browser Object Model (BOM) is used to interact with the browser.

  • This enables access & manipulation of the browser window, frames, and other browser-related objects by facilitating the JavaScript code.

  • The default object of browser is window means you can call all the functions of window by specifying window or directly

window.alert(" Hey Geeks"); 
// Same as
alert(" Hey Geeks ");

It is not recommended always to explicitly mention “window” because it’s the default. So, the alert(” Hey Geeks “); is equivalent to the longer version.

javascript object model

Browser Object Model Types :

TypesDescription
Window Object ModelRepresents the browser window and serves as the main tool for interaction within the Browser Object Model (BOM).
History Object ModelEnables navigation control by keeping track of the user’s browsing history in the Browser Object Model (BOM).
Screen Object ModelOffers information about the user’s screen, like its size, within the Browser Object Model (BOM).
Navigator Object ModelProvides details about the user’s browser and system characteristics within the Browser Object Model (BOM).
Location Object ModelManages the current web address (URL) and allows changes within the Browser Object Model (BOM).

Advantages & Disadvantages of BOM :

Advantages

  • User Interaction: BOM allows websites to show alerts, prompts, and confirmations, making them more interactive.

  • Navigation Control: It also helps to manage going back or forward in your browsing history using tools like ‘history.’

  • Browser Details: BOM gives information about your browser through the ‘navigator’ tool, helping to adjust content for different browsers and systems.

  • Location Management: Using the ‘location’ tool, BOM lets websites change web addresses for dynamic content and easy redirection.

Disadvantages

  • Browser Compatibility: It may work differently in various browsers, causing compatibility issues.

  • Security Concerns: Some BOM features, like pop-ups, can be exploited for security risks.

  • Client-Side Dependency: It relies heavily on the client side, hence affecting performance.

  • Limited Standardization: It lacks a standardized specification, leading to variations in implementation.

Window Object :

  • The window object represents a window in browser.

  • An object of window is created automatically by the browser.

  • Window is the object of browser, it is not the object of javascript.

  • The javascript objects are string, array, date etc.

Methods of window object :

The important methods of window object are as follows:

MethodDescription
alert()displays the alert box containing message with ok button.
confirm()displays the confirm dialog box containing message with ok and cancel button.
prompt()displays a dialog box to get input from the user.
open()opens the new window.
close()closes the current window.
setTimeout()performs action after specified time like calling function, evaluating expressions etc.

Example of alert() in javascript

It displays alert dialog box. It has message and ok button.

<script type="text/javascript">  
function msg(){  
 alert("Hello Alert Box");  
}  
</script>  
<input type="button" value="click" onclick="msg()"/>

History Object :

  • The JavaScript history object represents an array of URLs visited by the user.

  • By using this object, you can load previous, forward or any particular page.

  • The history object is the window property, so it can be accessed by:

    window.history

    Or

    history

Property of JavaScript history object :

There are only 1 property of history object.

No.PropertyDescription
1lengthreturns the length of the history URLs.

Methods of JavaScript history object :

There are only 3 methods of history object.

No.MethodDescription
1forward()loads the next page.
2back()loads the previous page.
3go()loads the given page number.

Example of history object :

Let’s see the different usage of history object

  1. history.back();//for previous page

  2. history.forward();//for next page

  3. history.go(2);//for next 2nd page

  4. history.go(-2);//for previous 2nd page

Navigator Object :

  • The JavaScript navigator object is used for browser detection.

  • It can be used to get browser information such as appName, appCodeName, userAgent etc.

  • The navigator object is the window property, so it can be accessed by

    window.navigator

    Or

    navigator

Property of JavaScript navigator object :

There are many properties of navigator object that returns information of the browser.

No.PropertyDescription
1appNamereturns the name
2appVersionreturns the version
3appCodeNamereturns the code name
4cookieEnabledreturns true if cookie is enabled otherwise false
5userAgentreturns the user agent
6languagereturns the language. It is supported in Netscape and Firefox only.
7userLanguagereturns the user language. It is supported in IE only.
8pluginsreturns the plugins. It is supported in Netscape and Firefox only.
9systemLanguagereturns the system language. It is supported in IE only.
10mimeTypes[]returns the array of mime type. It is supported in Netscape and Firefox only.
11platformreturns the platform e.g. Win32.
12onlinereturns true if browser is online otherwise false.

Methods of JavaScript navigator object :

The methods of navigator object are given below.

No.MethodDescription
1javaEnabled()checks if java is enabled.
2taintEnabled()checks if taint is enabled. It is deprecated since JavaScript 1.2.

Example:

<html>
<body>
<h2>JavaScript Navigator Object</h2>
<script>
document.writeln("<br/>navigator.appCodeName: "+navigator.appCodeName);
document.writeln("<br/>navigator.appName: "+navigator.appName);
document.writeln("<br/>navigator.appVersion: "+navigator.appVersion);
document.writeln("<br/>navigator.cookieEnabled: "+navigator.cookieEnabled);
document.writeln("<br/>navigator.language: "+navigator.language);
document.writeln("<br/>navigator.userAgent: "+navigator.userAgent);
document.writeln("<br/>navigator.platform: "+navigator.platform);
document.writeln("<br/>navigator.onLine: "+navigator.onLine);

</script>
</body>
</html>

Output:

Screen Object :

  • The JavaScript screen object holds information of browser screen.

  • It can be used to display screen width, height, colorDepth, pixelDepth etc.

The navigator object is the window property, so it can be accessed by:

window.screen Or screen

Property of JavaScript Screen Object :

There are many properties of screen object that returns information of the browser.

No.PropertyDescription
1widthreturns the width of the screen
2heightreturns the height of the screen
3availWidthreturns the available width
4availHeightreturns the available height
5colorDepthreturns the color depth
6pixelDepthreturns the pixel depth.

Example of JavaScript Screen Object

Let’s see the different usage of screen object.

<html>
<body>
<script>  
document.writeln("<br/>screen.width: "+screen.width);  
document.writeln("<br/>screen.height: "+screen.height);  
document.writeln("<br/>screen.availWidth: "+screen.availWidth);  
document.writeln("<br/>screen.availHeight: "+screen.availHeight);  
document.writeln("<br/>screen.colorDepth: "+screen.colorDepth);  
document.writeln("<br/>screen.pixelDepth: "+screen.pixelDepth);  
</script>
</body>
</html>

Output:

Whats Next ?

Document Object Model (DOM)