-1

I am trying to use the answer at https://stackoverflow.com/a/48240128/583363.

Open a window with window.open and then start an interval to check if it is closed. The problem is the closed property gets sets to true without the user closing the window.

Here is the code that shows the problem.

$('#content').on('click', '.myShareButton', function () {
  const newWindow = window.open('https://google.com', '_blank', 'width=200,height=200,scrollbars=no,status=no');
  console.log('window.closed', newWindow.closed); // prints false as expected
  if (newWindow) {
    const timer = setInterval(function () {
      console.log('window.closed', newWindow.closed); // prints false until closed gets set to true times
      if (newWindow.closed) {
        clearInterval(timer);
        console.log('window.closed', newWindow.closed); // prints true
      }
    }, 1);
  }
  return false;
});

Output from chrome devtools

enter image description here

Barış Uşaklı
  • 13,227
  • 7
  • 39
  • 65
  • 1
    This work on my end. See the [codepen](https://codepen.io/Samathingamajig/pen/mdXrGpO?editors=1111). If you are referring to the two "true" outputs, it's because you have a second console.log after the `clearInterval`, and the c.l before the if statement prints out the first true which makes it go into the if – Samathingamajig May 12 '22 at 22:54
  • So weird, works fine in codepen as in it will print "false" until the window is closed manually. When I put the code in my app it just prints "true" on it's own. – Barış Uşaklı May 12 '22 at 23:05

0 Answers0