HTML DOM defaultView Property


The HTML DOM defaultView property is used for returning the window object that is associated with the current document that is opened in the window.

Syntax

Following is the syntax −

document.defaultView

Example

Let us look at an example for the HTML DOM defaultView property −

Live Demo

<!DOCTYPE html>
<html>
<body>
<h2>defaultView Property example</h2>
<p>Click the below button to get information about the window</p>
<button onclick="winInfo()">Window Info</button>
<p id="Sample"></p>
<script>
   function winInfo() {
      var win = document.defaultView;
      var top = win.screenTop;
      var left = win.screenLeft;
      var name = win.name;
      document.getElementById("Sample").innerHTML = "Distance from screen top: " + top + "<br> Distance from screen left: "+left+"<br> Window name: "+name;
   }
</script>
</body>
</html>

Output

This will produce the following output −

On clicking the Window Info button −

In the above example −

We have first created a button “Window Info” that will execute the winInfo() function when clicked by the user −

<button>Window Info</button>

The winInfo() function gets the document’s window object using the document defaultView property and assigns it to the variable win. We then get the window object screenTop, screenLeft and name property values and assigns it to variable top, left and name respectively.

The screenLeft and screenTop displays the distance of the browser window from top and left of the screen, respectively. The name property returns the name of the window which can be null −

function winInfo() {
   var win = document.defaultView;
   var top = win.screenTop;
   var left = win.screenLeft;
   var name = win.name;
   document.getElementById("Sample").innerHTML = "Distance from screen top: " + top + "<br> Distance from screen left: "+left+"<br> Window name: "+name;
}

Updated on: 15-Feb-2021

34 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements