title: Detect if new window if opend using javascript
body: <p>Creating a small chat application where user can start chat by clicking on chat button next to user's profile.</p>
  
  <p>Once anyone will click on chat button a popup window will appear. Now I want to detect dynamically if this popus is still open or closed. </p>
  
  <p>If popup is opened= Display new message alert in notification.</p>
  
  <p>if popup is closed = Do not alert new message in notification (As chat window is already opened)</p>
  
  <p>This is what I tried:</p>
  
  <pre><code>    &lt;a href="#" onclick="popitup("http://domain.com/chat/2")"&gt;Chat with user 2&lt;/a&gt;
      &lt;a href="#" onclick="popitup("http://domain.com/chat/1")"&gt;Chat with user 1&lt;/a&gt;
  </code></pre>
  
  <pre class="lang-js prettyprint-override"><code>// on document ready
  setInterval(function(){
      chkAlerts();
  }, 10000);
  
  var newwindow = null;
  
  function popitup(url) {
      if ((newwindow == null) || (newwindow.closed)) {
          newwindow = window.open(url,'Buy','width=950,height=650,scrollbars=yes,resizable=yes');
          newwindow.focus();
      } 
      else {
          newwindow.location.href = url;
          newwindow.focus();    
      }
  }  
  
  function chkAlerts(){
      if ((newwindow == null) || (newwindow.closed)) {
          //Do something
          alert('now window is closed show alerts');
      }
  }
  </code></pre>
  
  <p>With this code I am always getting </p>
  
  <pre><code>alert('now window is closed show alerts');
  </code></pre>