Javascript Browser Window closed Property

Introduction

A function that checks whether a window called myWindow has been closed or not:

View in separate window

<!DOCTYPE html>
<html>
<head>
<script>
var myWindow;/*from  w w w  .j  a  v  a  2s.  co m*/

function openWin() {
  myWindow = window.open("", "myWindow", "width=400, height=200");
}

function closeWin() {
  if (myWindow) {
    myWindow.close();
  }
}

function checkWin() {
  if (!myWindow) {
    document.getElementById("msg").innerHTML = "'myWindow' has never been opened!";
  } else {
    if (myWindow.closed) {
      document.getElementById("msg").innerHTML = "'myWindow' has been closed!";
    } else {
      document.getElementById("msg").innerHTML = "'myWindow' has not been closed!";
    }
  }
}
</script>
</head>
<body>

<button onclick="openWin()">Open "myWindow"</button>
<button onclick="closeWin()">Close "myWindow"</button>
<br><br>
<button onclick="checkWin()">Has "myWindow" been closed?</button>
<br><br>
<div id="msg"></div>

</body>
</html>

The closed property returns a Boolean value indicating whether a window has been closed or not.

Syntax

Return Value: A Boolean, true if the window has been closed, or false if the window is open



PreviousNext

Related