window.open() reference is having closed true, after perform submit in child window without closing child in chrome only

446 views Asked by At

when i open child window and search something in search box and when see the 'child.closed' status giving different results in Firefox and Chrome. Please help to get the same status in both browsers. Firefox: 'child.closed' returns false Chrome: 'child.closed' returns true

<html lang="en">

<head>
    <title>Sample page</title>
    <script>
        function openChild() {
            var child = window.open('http://google.com', 'auditor_child_tab', 'location=yes,status=yes,width=1366,height=768');
            const clearTimeout = setInterval(() => {
                if (child && child.closed) {
                    clearInterval(clearTimeout)
                    console.log("THE CHILD WINDOW HAS BEEN CLOSED")
                } else {
                    console.log("OK")
                }
            }, 1000)
        }
    </script>
</head>

<body>
    <h1>Sample Page</h1>
    <button onclick="openChild()">Open Child</button>
</body>

</html>
1

There are 1 answers

1
omi On

window.closed property should be giving the same response in both browsers. This is the standard way to check window is still opened. I see one problem in your code. You should also check whether the window object is null or not.

function openChild() {
            var child = window.open('http://google.com', 'auditor_child_tab', 'location=yes,status=yes,width=1366,height=768');
            const clearTimeout = setInterval(() => {
                if (child === null || child.closed) {
                    clearInterval(clearTimeout)
                    console.log("THE CHILD WINDOW HAS BEEN CLOSED")
                } else {
                    console.log("OK")
                }
            }, 1000)
        }

Regards, omi