Window.closed is a read-only Boolean property on a referenced window. It is true when that window is closed and false while it remains open.
if (childWindow && !childWindow.closed) {
childWindow.focus();
}The original page linked to Ajax Message; that related reference is retained. The updated demo itself uses a small inline status region and does not require Ajax.
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Demo of closed window</title></head>
<body>
<button type="button" id="openWindow">Open</button>
<button type="button" id="closeWindow">Close</button>
<button type="button" id="focusWindow">Focus</button>
<button type="button" id="blurWindow">Blur</button>
<p id="msg" hidden></p>
<script>
let childWindow = null;
let hideTimer = null;
const message = document.getElementById('msg');
function showMessage(text) {
message.textContent = text;
message.hidden = false;
clearTimeout(hideTimer);
hideTimer = setTimeout(() => { message.hidden = true; }, 2000);
}
function isUnavailable() {
return !childWindow || childWindow.closed;
}
document.getElementById('openWindow').addEventListener('click', () => {
childWindow = window.open('moveby-child.htm', 'Ratting', 'width=250,height=150,left=150,top=200,status=0');
showMessage(childWindow ? 'Window is open' : 'Popup blocked. Allow popups and try again.');
});
document.getElementById('closeWindow').addEventListener('click', () => {
if (isUnavailable()) return showMessage('Window is closed, first open it');
childWindow.close();
showMessage('Close requested');
});
document.getElementById('focusWindow').addEventListener('click', () => {
if (isUnavailable()) return showMessage('Window is closed, first open it');
childWindow.focus();
showMessage('Window focus requested');
});
document.getElementById('blurWindow').addEventListener('click', () => {
if (isUnavailable()) return showMessage('Window is closed, first open it');
childWindow.blur();
showMessage('Window blur requested (browser may ignore it)');
});
</script>
</body>
</html>Before reading .closed, first make sure a window reference exists. A blocked window.open() call can return null.
const childWindow = window.open("child.htm", "child");
if (!childWindow) {
console.log("Popup was blocked");
} else if (childWindow.closed) {
console.log("The child is closed");
}The original demo has Open, Close, Focus and Blur controls, and all four are preserved. Browsers may restrict or ignore focus/blur requests to avoid disruptive window behavior.
Window Object Bookmark a page Redirect Page
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.