The Browser Object Model (BOM) is a way to interact with browser. It deals with browser components aside from the document, like history, location, navigator, frame and screen.
The default object is window i.e. it is not necessary to use window object with function, the browser automatically uses it.
For example:
Syntax
window.prompt("hello coderepublics");
It is same as :
prompt("hello coderepublics");
Javascript Window Object
If a window is open in a browser then browser uses a window object to represent that window.
Every opened window/frame/dialog box has a window object.
It is created automatically by the browser.
Javascript Window Object Methods
Property
Description
alert()
It displays the alert box.
confirm()
It displays a confirm dialog box.
prompt()
It displays a dialog box.
setTimeout()
It performs action after a specified time.
open()
It opens the new window.
close()
It closes the window.
We'll learn about the dialog boxes later. Below are some examples of other functions.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title> JavaScript Viewport Dimension </title>
</head>
<body>
<script>
function windowSize(){
var w = window.innerWidth;
var h = window.innerHeight;
alert("Width: " + w + ", " + "Height: " + h);
}
</script>
<button type="button" onclick="windowSize();">Get Window Size</button>
</body>
</html>
Output
JavaScript Viewport Dimension
The width and height of any element can also be calculated by clientWidth and clientHeight but this length will be
excluding the scrollbars(if any) unlike previous ones which includes the scrollbar length too.