JavaScript- BOM Concept
JavaScript- BOM Concept
The Browser Object Model (BOM) in JavaScript refers to the objects provided by the browsers
to interact with them. By using these objects, you can manipulate the browser's functionality. For
example, you can get the browser history and window size, navigate to different URLs, etc.
The Browser object model is not standardized. It depends on which browser you are using.
Here, we have listed all objects of the Browser Object Model with descriptions −
1. Window − The 'window' object represents the current browser window. You
can use it to manipulate the browser window.
6. Location − The Location object is used to get the URL information, such as
the hostname of the current web page.
All the JavaScript global variables are properties of window object. All global functions are
methods of the window object.
The other objects listed above such as document, screen, history etc., are the properties of the
window object. We can access these objects as properties of the window object. We can also
access them with referencing the window object. Look at the below example snippet to access
the document object
The innerHeight and innerWidth properties of the window object are used to access the height
and width of the browser window. We will learn JavaScript window object in detail in next
chapters.
The document object provides us with many properties and methods to access the HTML
elements and manipulate them. One such method is document.getElementById() to access the
HTML element using its id.
We can access an element with id as "text" using getElementById() method and manipulate it
Example
<html>
<body>
<div id ="text">This text will be changed. </div>
<script>
// Access the element with id as text
const textDiv = document.getElementById("text");
// Change the content of this element
textDiv.innerHTML = "The text of this DIV is changed.";
</script>
</body>
</html>
JavaScript Screen Object
In JavaScript, the screen object is a property of window object. It provides us with properties that
can be used to get the information about the device window screen. We can access the screen
object as window.screen or just screen.
To get the width and height of the device screen in pixels we can use the screen.width and
screen.height properties
Example
<html>
<body>
<div id ="width">Width of the device screen is </div>
<div id ="height">Height of the device screen is </div>
<script>
document.getElementById("width").innerHTML += screen.width + " px.";
document.getElementById("height").innerHTML += screen.height + " px.";
</script>
<p> The above result may be different for different device.</p>
</body>
</html>
Output
Width of the device screen is 1536 px.
Height of the device screen is 864 px.
The above result may be different for different device.
The JavaScript history object can be accessed using window.history or just history. We can use
the methods of history object to navigate the URLs in the history list. For example to go the
previous page/URL in the history list, we can use history.back() method.
We can use the properties and methods of the location object to manipulate the URL
information. For example, to get the host from the current URL, we can use the
window.location.host or just location.host. The host is property of the location object.
Example
<html>
<body>
<div id = "output"> </div>
<script>
document.getElementById("output").innerHTML =
"The host of the current location is: " + location.host;
</script>
</body>
</html>
Output
The host of the current location is: www.tutorialspoint.com
The console object provides us with different methods such as console.log(). The console.log()
method is used to display the message in debugging console.
JavaScript - Window Object
The JavaScript window object represents the browser's window. In JavaScript, a 'window' object
is a global object. It contains the various methods and properties that we can use to access and
manipulate the current browser window. For example, showing an alert, opening a new window,
closing the current window, etc.
All the JavaScript global variables are properties of window object. All global functions are
methods of the window object. Furthermore, when the browser renders the content in the 'iframe,
it creates a separate 'window' object for the browser and each iframe.
Here, you will learn to use the 'window' object as a global object and use the properties and
methods of the window object.
As 'window' is a global object in the web browser, you can access the global variables, functions,
objects, etc., using the window object anywhere in the code.
Example
In the below code, we have defined the 'num' global and local variables inside the function. Also,
we have defined the 'car' global object.
In the test() function, we access the global num variable's value using the 'window' object.
<html>
<body>
<div id = "output1">The value of the global num variable is: </div>
<div id = "output2">The value of the local num variable is: </div>
<div id = "output3">The value of the car object is: </div>
<script>
var num = 100;
const car = {
brand: "Honda",
model: "city",
}
function test() {
let num = 300;
document.getElementById("output1").innerHTML += window.num;
document.getElementById("output2").innerHTML += num;
document.getElementById("output3").innerHTML += car.brand;
}
test();
</script>
</body>
</html>
Output
The value of the global num variable is: 100
The value of the local num variable is: 300
The value of the car object is: Honda
You may also use the 'window' object to make a particular variable global from a particular
block.
The 'window' object contains the various properties, returning the status and information about
the current window.
Below, we have covered all properties of the 'window' object with a description. You may use
the 'window' as a reference to access these properties.
devicePixelRatio It returns the physical pixel ratio of the device divided by CSS pixel ratio.
document It is used to access the HTML document opened in the current window.
frames It is used to get the window items like iframes, which are opened in the
current window.
It returns the inner height of the window without including the scroll bar,
innerHeight
toolbar, etc.
It returns the inner width of the window without including the scroll bar,
innerWidth
toolbar, etc.
It returns the number of pixels you have scrolled the web page
pageXOffset
horizontally.
pageYOffset It returns the number of pixels you have scrolled the web page vertically.
parent It contains the reference to the parent window of the current window.
scheduler It is entry point for using the prioritized task scheduling.
sessionStorage It lets you access the 'sessionStorage' object of the current window.
visualViewPort It returns the object containing the viewport of the current window.
The outerHeight property of the window object returns the window's height, and the outerWidth
property of the window object returns the window's width.
Example
In the code below, we used the outerHeight and outerWidth property to get the dimensions of the
window. You can change the size of the window and observe changes in the value of these
properties.
<html>
<body>
<p id = "output1">The outer width of the window is: </p>
<p id = "output2">The outer height of the window is: </p>
<script>
const outerHeight = window.outerHeight;
const outerWidth = window.outerWidth;
document.getElementById("output1").innerHTML += outerWidth;
document.getElementById("output2").innerHTML += outerHeight;
</script>
</body>
</html>
Output
The outer width of the window is: 1536
The outer height of the window is: 816
The window screenLeft property returns the left position of the current window.
Example
In the output of the below code, you can see the left position of the current window in pixels.
<html>
<body>
<div id = "output">Your browser window is left by: </div>
<script>
const screenLeft = window.screenLeft;
document.getElementById("output").innerHTML += screenLeft + " px.";
</script>
</body>
</html>
Output
Your browser window is left by: 0 px.
Window Object Methods
The 'window' object also contains methods like properties to manipulate the current browser
window.
In the below table, we have covered the methods of the 'window' object with their description.
You may use 'window' as a reference to access and invoke the below methods to make the code
readable.
clearInterval() It resets the timer you have set using the setInterval() method.
confirm() It shows the confirm box to get the confirmation from users.
focus() It focuses on the current active window.
getSelection() It returns the selection object based on the selected text range.
The window.alert() method allows you to show the pop-up dialog containing the message,
warning, etc. It takes the string text as an argument.
Example
In the below example, when you click the button, it will invoke the alert_func() function and
show the pop-up box at the middle top.
<html>
<body>
<button onclick = "alert_func()"> Execute Alert </button>
<script>
function alert_func() {
window.alert("The alert_func funciton is executed!");
}
</script>
</body>
</html>
The window.open() method is used to open a new window. It takes a URL as an argument,
which you need to open in a new window.
Example
In the below code, we used the window.open() method to open a new window in the browser.
You can see that the code opens the home page of the 'tutorialspoint' website in the new window.
<html>
<body>
<button onclick = "openWindow()"> Open New Window </button>
<script>
function openWindow() {
window.open("https://www.tutorialspoint.com/");
}
</script>
</body>
</html>
Example
In the below code, click the button to print the web page.
<html>
<body>
<h2> Hello World! </h2>
<button onclick = "printPage()"> Print Webpage </button>
<script>
function printPage() {
window.print();
}
</script>
</body>
</html>
JavaScript - Document Object
Window Document Object
The document object is a JavaScript object that provides the access to all elements of an HTML
document. When the HTML document is loaded in the web browser, it creates
a document object. It is the root of the HTML document.
The document object contains the various properties and methods you can use to get details
about HTML elements and customize them.
The JavaScript document object is a property of the window object. It can be accessed
using window.document syntax. It can also be accessed without prefixing window object.
These properties provide a way to access and modify different parts of the HTML document
using JavaScript.
<html>
<head>
<title> JavaScript - DOM Object </title>
</head>
<body>
<div id = "output">The title of the document is: </div>
<script>
document.getElementById("output").innerHTML += document.title;
</script>
</body>
</html>
Output
The title of the document is: JavaScript - DOM Object
<html>
<head>
<title> JavaScript - DOM Object </title>
</head>
<body>
<div id = "output">The URL of the document is: </div>
<script>
document.getElementById("output").innerHTML += document.URL;
</script>
</body>
</html>
Output
The URL of the document is:
https://www.tutorialspoint.com/javascript/javascript_document_object.htm
These methods enable us to dynamically manipulate the structure and content of the HTML
document using JavaScript.
<html>
<body>
<div id = "result"> </div>
<script>
// accessing the DIV element.
document.getElementById("result").innerHTML +=
"Hello User! You have accessed the DIV element using its id.";
</script>
</body>
</html>
Output
Hello User! You have accessed the DIV element using its id.
<html>
<body>
<div id = "result">
<h2> Mouseover Event </h2>
<p> Hover over here to change background color </p>
</div>
<script>
document.addEventListener('mouseover', function () {
document.getElementById("result").innerHTML = "Mouseover event occurred.";
});
</script>
</body>
</html>
Property Description
lastModified To get the last modified date and time of the document.
Method Description
It appends the new node or HTML after the last child node of
append()
the document.
createElement() It creates a new element node to insert into the web page.
It removes the text nodes, which are empty, and joins other
normalize()
nodes.
It is used to select the first element that matches the css selector
querySelector()
passed as an argument.
window.screen.property;
OR
screen.property;
You may or may not use the 'window' object to access the screen object.
Example
In the example below, we access the various properties of the screen object with referencing
the screen as the property of window object.
<html>
<body>
<p> Screen Information </p>
<div id = "output"> </div>
<script>
document.getElementById("output").innerHTML =
"screen height: " + window.screen.height + "<br>" +
"screen width: " + window.screen.width + "<br>" +
"screen colorDepth: " + window.screen.colorDepth + "<br>" +
"screen pixelDepth: " + window.screen.pixelDepth + "<br>" +
"screen availHeight: " + window.screen.availHeight + "<br>" +
"screen availWidth: " + window.screen.availWidth;
</script>
</body>
</html>
Output
Screen Information
screen height: 864
screen width: 1536
screen colorDepth: 24
screen pixelDepth: 24
screen availHeight: 816
screen availWidth: 1536
Example
In the below code, we access the various properties of the screen object and observe their value.
In this example we are not referencing the window object to access the screen object.
<html>
<body>
<p> Screen Information </p>
<div id = "output"> </div>
<script>
document.getElementById("output").innerHTML =
"screen height: " + screen.height + "<br>" +
"screen width: " + screen.width + "<br>" +
"screen colorDepth: " + screen.colorDepth + "<br>" +
"screen pixelDepth: " + screen.pixelDepth + "<br>" +
"screen availHeight: " + screen.availHeight + "<br>" +
"screen availWidth: " + screen.availWidth;
</script>
</body>
</html>
Output
Screen Information
screen height: 864
screen width: 1536
screen colorDepth: 24
screen pixelDepth: 24
screen availHeight: 816
screen availWidth: 1536
Please note that we get the same information about the screen in the above two examples.
Property Description
availHeight It gives the height of the screen without including the taskbar.
availWidth It gives the width of the screen without including the taskbar.
The 'history' object is a property of the 'window' object. The history property can be accessed
with or without referencing its owner object, i.e., window object.
Using the 'history' object's method, you can go to the browser's session's previous, following, or
particular URL.
window.history.methodName();
OR
history.methodName();
You may use the 'window' object to access the 'history' object.
Syntax
Follow the syntax below to use the history back() method.
history.back();
Example
In the below code's output, you can click the 'go back' button to go to the previous URL. It works
as a back button of the browser.
<html>
<body>
<p> Click "Go Back" button to load previous URL </p>
<button onclick="goback()"> Go Back </button>
<p id = "output"> </p>
<script>
function goback() {
history.back();
document.getElementById("output").innerHTML +=
"You will have gone to previous URL if it exists";
}
</script>
</body>
</html>
Syntax
Follow the syntax below to use the forward() method.
history.forward();
Example
In the below code, click the button to go to the next URL. It works as the forward button of the
browser.
<html>
<body>
<p> Click "Go Forward" button to load next URL</p>
<button onclick = "goforward()"> Go Forward </button>
<p id = "output"> </p>
<script>
function goforward() {
history.forward();
document.getElementById("output").innerHTML =
"You will have forwarded to next URL if it exists."
}
</script>
</body>
</html>
JavaScript History go() Method
The go() method of the history object takes you to the specific URL of the browser's session.
Syntax
Follow the syntax below to use the go() method.
history.go(count);
In the above syntax, 'count' is a numeric value representing which page you want to visit.
Example
In the below code, we use the go() method to move to the 2nd previous page from the current
web page.
<html>
<body>
<p> Click the below button to load 2nd previous URL</p>
<button onclick = "moveTo()"> Move at 2nd previous URL </button>
<p id = "output"> </p>
<script>
function moveTo() {
history.go(-2);
document.getElementById("output").innerHTML =
"You will have forwarded to 2nd previous URL if it exists.";
}
</script>
</body>
</html>
Example
In the below code, we use the go() method to move to the 2nd previous page from the current
web page.
Open Compiler
<html>
<body>
<p> Click the below button to load 2nd next URL</p>
<button onclick = "moveTo()"> Move at 2nd next URL </button>
<p id = "output"> </p>
<script>
function moveTo() {
history.go(2);
document.getElementById("output").innerHTML =
"You will have forwarded to 2nd next URL if it exists.";
}
</script>
</body>
</html>
Following is the complete window history object reference including both properties and
methods −
Property Description
It returns the object's length, representing the number of URLS present in the
length
object.
Method Description
The 'navigator' object is a property of the window object. The navigator object can be accessed
using the read-only window.navigator property.
Syntax
Follow the syntax below to use to access a property of navigator object in JavaScript.
window.navigator.proeprty;
OR
navigator.property;
You may use the 'window' object to access the 'navigator' object.
Property Description
language It returns the browser language. It is supported by Firefox and Netscape only.
plugins It returns the browser plugins. It is supported by Firefox and Netscape only.
platform It gives you a platform or operating system in which the browser is used.
The appName property returns the browser's name, appCodeName returns the code name of the
browser, appVersion returns the browser's version, and the cookieEnabled property checks
whether the cookies are enabled in the browser.
<html>
<body>
<p> Browser Information</p>
<p id = "demo"> </p>
<script>
document.getElementById("demo").innerHTML =
"App Name: " + navigator.appName + "<br>" +
"App Code Name: " + navigator.appCodeName + "<br>" +
"App Version: " + navigator.appVersion + "<br>" +
"Cookie Enabled: " + navigator.cookieEnabled + "<br>" +
"Language: " + navigator.language + "<br>" +
"Plugins: " + navigator.plugins + "<br>" +
"mimeTypes[]: " + navigator.mimeTypes + "<br>" +
"platform: " + navigator.platform + "<br>" +
"online: " + navigator.online + "<br>" +
"product: " + navigator.product + "<br>" +
"userAgent: " + navigator.userAgent;
</script>
<p>Please note you may get different result depending on your browser. </p>
</body>
</html>
Output
Browser Information
Please note you may get different result depending on your browser.
Example
In the example below, we accessed the navigator object as a property of the window object. Then
we accessed different properties of this navigator object.
<html>
<body>
<p> Browser Information</p>
<p id = "demo"> </p>
<script>
document.getElementById("demo").innerHTML =
"App Name: " + window.navigator.appName + "<br>" +
"App Code Name: " + window.navigator.appCodeName + "<br>" +
"App Version: " + window.navigator.appVersion + "<br>" +
"Cookie Enabled: " + window.navigator.cookieEnabled + "<br>" +
"Language: " + window.navigator.language + "<br>" +
"Plugins: " + window.navigator.plugins + "<br>" +
"mimeTypes[]: " + window.navigator.mimeTypes + "<br>" +
"platform: " + window.navigator.platform + "<br>" +
"online: " + window.navigator.online + "<br>" +
"product: " + window.navigator.product + "<br>" +
"userAgent: " + window.navigator.userAgent;
</script>
<p>Please note you may get different result depending on your browser. </p>
</body>
</html>
Output
Browser Information
Please note you may get different result depending on your browser.
Method Description
<html>
<body>
<p id = "output"> </p>
<script>
const output = document.getElementById("output");
if (navigator.javaEnabled()) {
output.innerHTML += "Java is enabled in the browser!";
} else {
output.innerHTML += "Please, enable the Java!";
}
</script>
<p>Please note you may get different result depending on your browser. </p>
</body>
</html>
Output
Please, enable the Java!
Please note you may get different result depending on your browser.
Window Location Object
The location object in JavaScript provides information about the browser's location, i.e., URLs.
It is a built-in property of both the window and document objects. We can access it using
either window.location or document.location.
The 'location' object contains various properties and methods to get and manipulate the
information of the browser's location (URL).
We can use the properties of the location object to get information of URL:
• hash − This property is used to set or get the anchor part of the URL.
• host − This property is used to set or get the hostname or port number of the URL.
• hostname − This property is used to set the hostname.
• href − This property is used to set or get the URL of the current window.
• origin − This property returns the protocol, domain, and port of the URL.
• pathname − This property updates or gets the path name.
• port − This property updates or gets the port of the URL.
• protocol − This property updates or gets the protocol.
• search − This property is used to set or get the query string of the URL.
Syntax
Follow the syntax below to access the location object's properties and methods −
window.location.property;
OR
location.property;
You may use the 'window' object to access the 'location' object.
Here, we have demonstrated the use of some properties of the location object with examples.
Example: Accessing location host property
The location.host property returns the host from the current URL. However, you can also
change the host using it.
In the below code, we extract the host from the URL. You can see that it returns
'www.tutorialspoint.com'.
<html>
<body>
<div id="output"></div>
<script>
const host = location.host;
document.getElementById("output").innerHTML =
"The host of the current location is: " + host;
</script>
</body>
</html>
Output
The host of the current location is: www.tutorialspoint.com
<html>
<body>
<div id = "output"> </div>
<script>
document.getElementById("output").innerHTML =
"The protocol of the current location is: " + location.protocol;
</script>
</body>
</html>
Output
The protocol of the current location is: https:
Open Compiler
<html>
<body>
<div id = "output"> </div>
<script>
document.getElementById("output").innerHTML =
"The host name of the current location is: " + location.hostname;
</script>
</body>
</html>
Output
The host name of the current location is: www.tutorialspoint.com
Open Compiler
<html>
<body>
<div id = "output"> </div>
<script>
document.getElementById("output").innerHTML =
"The protocol of the current location is: " + location.pathname;
</script>
</body>
</html>
Output
The protocol of the current location is: /javascript/javascript_location_object.htm
Syntax
Follow the syntax below to use the location.assign() method in JavaScript −
location.assign();
Example
In the below code, when you click the 'Go to home page' button, it will redirect you to the home
page of the tutorialpoint website.
Open Compiler
<html>
<body>
<div id="output"></div>
<button onclick="changePage()">Go to Home Page</button>
<script>
let output = document.getElementById("output");
function changePage() {
window.location.assign("https://www.tutorialspoint.com/");
}
</script>
</body>
</html>
Property Description
It is used to set or get the anchor part of
hash
the URL.
Method Description
The console object contains the various methods used for different functionalities. In Node.js, the
console object is used to interact with the terminal.
We access the console object using the window object or without using the window object -
window.console or just console.
You may access the 'console' object methods using the following syntax −
window.console.methodName();
OR
console.methodName();
You can observe outputs in the console. To open the console, use the Ctrl + shift + I or Cmd +
shift + I key.
Below, we will cover some methods of the console object with examples.
JavaScript console.log() Method
You can use the console.log() method to print the message in the debugging console. It takes the
expression or text message as an argument.
Syntax
Follow the syntax below to use the console.log() method.
console.log(expression);
In the above syntax, the expression can be a variable, mathematical expression, string, etc.,
which you need to print in the console.
Example
In the below code, clicking the button will invoke the 'printMessage' function. The function
prints the string text and number value in the console.
Open Compiler
<html>
<body>
<h2> JavaScript console.log() Method </h2>
<button onclick = "printMessage()"> Print Message in Console </button>
<p> Please open the console before you click "Print Message in Console" button</p>
<script>
function printMessage() {
console.log("You have printed message in console!");
let num = 10;
console.log(num);
}
</script>
</body>
</html>
Syntax
Follow the syntax below to use the console.error() method.
console.error(message);
Example
In the below code, printError() function logs the error in the console when you click the button.
You can observe the error highlighted with the red background.
Open Compiler
<html>
<body>
<h2> JavaScript console.error() Method </h2>
<button onclick="printError()"> Print Error Message in Console </button>
<p> Please open the console before you click " Print Error Message in Console" button.</p>
<script>
function printError() {
console.error("Error occured in the code!");
}
</script>
</body>
</html>
Syntax
Follow the syntax below to use the console.clear() method.
console.clear()
Example
In the below code, we print messages to the console. After that, when you click the button, it
executes the clearConsole() function and clears the console using the console.clear() method.
Open Compiler
<html>
<body>
<h2> JavaScript console.clear() Method </h2>
<button onclick = "clearConsole()"> Clear Console </button>
<p> Please open the console before you click "Clear Console" button</p>
<script>
console.log("Hello world!");
console.log("Click the button to clear the console.");
function clearConsole() {
console.clear();
}
</script>
</body>
</html>