HTML DOM Window parent Property


The HTML DOM Window parent property returns a reference to the parent window of the child window.

Syntax

Following is the syntax −

Returning reference of parent window

window.parent

Example

Let us see an example of HTML DOM Window parent property −

<!DOCTYPE html>
<html>
<head>
<title>HTML DOM Window parent</title>
<style>
   * {
      padding: 2px;
      margin:5px;
   }
   form {
      width:70%;
      margin: 0 auto;
      text-align: center;
   }
   input[type="button"] {
      border-radius: 10px;
</style>
</head>
<body>
   <form>
      <fieldset>
         <legend>HTML-DOM-Window-parent</legend>
         <input id="urlSelect" type="url" placeholder="Type URL here..."><br>
         <input type="button" value="Go To" onclick="openWindow()">
         <input type="button" value="Close" onclick="closeWindow()">
         <input type="button" value="Restore" onclick="restoreWindow()">
         <div id="divDisplay"></div>
      </fieldset>
   </form>
   <script>
      var urlSelect = document.getElementById("urlSelect");
      var winSource;
      function openWindow() {
         browseWindow = window.open(urlSelect.value, "browseWindow", "width=400, height=200");
         winSource = urlSelect.value;
         parent.document.getElementById("divDisplay").textContent = "Child Window Active";
      }
      function closeWindow(){
         if(browseWindow){
            browseWindow.close();
            parent.document.getElementById("divDisplay").textContent = "Child Window Closed";
         }
      }
      function restoreWindow(){
         if(browseWindow.closed){
            browseWindow = window.open(winSource, "browseWindow", "width=400, height=200");
            parent.document.getElementById("divDisplay").textContent = "Child Window Restored";
         }
      }
   </script>
</body>
</html>

Output

Clicking ‘Go To’ button with url field set −

Clicking ‘Close’ button −

Clicking ‘Restore’ button −

Updated on: 21-Dec-2021

649 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements