JavaScript > Window

closed Property in JavaScript

How to check whether a window has been closed or not in JavaScript?


closed Property

closed property is an boolean type property, which is used to check whther the browser window has been closed or not.

Checking whether the window is closed or not.

<p>Click the below buttons to know the closed property testing result.</p>
<input type="button" value="Click to open" onclick="testOpen()"/><br />
<input type="button" value="Click to close" onclick="testClose()"/><br /><br />
<input type="button" value="Click to check" onclick="testCheck()"/><br />
<p id="myId"></p>

<script>
    var newWindow;

    function testOpen() {
        newWindow = window.open("", "newWindow", "height=250, width=500");
    }

    function testClose() {
        if (newWindow) {
            newWindow.close();
        }
    }

    function testCheck() {
        if (!newWindow) {
            document.getElementById("myId").innerHTML = "Window is not yet opened!";
        } else {
            if (!newWindow.closed) {
                document.getElementById("myId").innerHTML = "Window is not closed.";
            } else {
                document.getElementById("myId").innerHTML = "Window is closed"
            }
        }
    }
</script>

In the script code function testOpen() opens the new window of a browser, function testClose() closes the opened window of a browser, function testCheck() tests the working of closed property and returns the output.

 Views: 5802 | Post Order: 195



Write for us






Hosting Recommendations