HTML - DOM Document defaultView Property



The defaultView property is used for returning document's window object. It provides a way to access the window object associated with HTML document. This property is a read-only property.

Syntax

document.defaultView;

Return value

It returns current document's object.

Examples of HTML DOM Document 'defaultView' Property

The following examples illustrates different uses of defaultView property.

Get the document's Window Object

The following example returns the document's window object.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM document defaultView Property
    </title>
</head>
<body>
    <button onclick="fun()">Click me</button>
    <p id="view"></p>
    <script>
        function fun() {
            let view = document.defaultView;
            document.getElementById("view").innerHTML = view;
        }
    </script>
</body>
</html>

Get Window's Dimension

The following example gives window's dimension as height and width of the window.

<!DOCTYPE html>
<html lang="en">
<head>
    <title>
        HTML DOM document defaultView Property
    </title>
</head>
<body>
    <p>
        Click the below button to get 
        the dimension of the window
    </p>
    <button onclick="fun()">Click me</button>
    <p id="info"></p>
    <script>
        function fun() {
            let x = document.defaultView;
            let height = x.innerHeight;
            let width = x.innerWidth;
            document.getElementById("info").innerHTML = "Window's height: "
                + height + "<br> Window's width: " + width;
        }
    </script>
</body>
</html>

Supported Browsers

Property Chrome Edge Firefox Safari Opera
defaultView Yes 1 Yes 12 Yes 1 Yes 1 Yes 12.1
html_dom.htm
Advertisements