Open In App

HTML DOM defaultView Property

Last Updated : 30 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

The DOM defaultView property in HTML is used to return the document Window Object. The window object is the open windows in the browser. 

Syntax:

document.defaultView 

Return Value: It is used to return the current Window Object. 

Example 1: In this example use the defaultView property to get the window object and display it in a <p> element when the button is clicked. The content is centered and styled.

html
<!DOCTYPE html>
<html>

<head>
    <title>
        DOM defaultView Property
    </title>
</head>

<body style="text-align:center">

    <h1 style="color:green">GeeksForGeeks</h1>
    <h2>DOM defaultView Property </h2>

    <button onclick="geeks()">Submit</button>

    <p id="sudo"></p>

    <script>
        function geeks() {
            let doc = document.defaultView;
            document.getElementById("sudo").innerHTML = doc;
        }
    </script>

</body>

</html>

Output:

Example 2: This example is used to return the width and height of the Windows. 

html
<!DOCTYPE html>
<html>

<head>
    <title>
        DOM defaultView Property
    </title>
</head>

<body>
    <center>
        <h1 style="color:green;">GeeksForGeeks</h1>
        <h2>DOM defaultView Property </h2>

        <button onclick="Geeks()">Submit</button>

        <p id="sudo"></p>

        <!-- script to find window size -->
        <script>
            function Geeks() {
                let def_view = document.defaultView;
                let width = def_view.innerWidth;
                let height = def_view.innerHeight;
                document.getElementById("sudo").innerHTML
                    = "Width: " + width +
                    "<br>Height: " + height;
            }
        </script>
    </center>
</body>

</html>

Output:

 Supported Browsers: The browser supported by DOM defaultView property are listed below:



Next Article
Article Tags :

Similar Reads