Javascript Reference - Window closed Property








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

Browser Support

closed Yes Yes Yes Yes Yes

Syntax

window.closed

Return Value

It returns a Boolean type value, 'true' if the window has been closed, or 'false' if the window is open.





Example

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

<!DOCTYPE html>
<html>
<head>
<script>
var myWindow;
function openWin() {<!--from   ww  w .  java  2s . co m-->
    myWindow = window.open("", "myWindow", "width=400, height=200");
}
function closeWin() {
    if (myWindow) {
        myWindow.close();
    }
}

function checkWin() {
    if (!myWindow) {
        document.getElementById("msg").innerHTML = "opened!";
    } else {
        if (myWindow.closed) { 
            document.getElementById("msg").innerHTML = "closed!";
        } else {
            document.getElementById("msg").innerHTML = "not closed!";
        }
    }  
}

</script>
</head>
<body>

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

Click to view the demo