title: Closing a JavaScript window with an onclick event
body: <p>We have to make a JavaScript that opens a new Window, and then you need to be able to close it again with a click inside the window.
  But my code does not work, could someone please provide me an answer to how this is done best?</p>
  
  <pre><code>function swipe() {
     var largeImage = document.getElementById('largeImage');
     var url = largeImage.getAttribute('src');
     var w = largeImage.naturalWidth;
     var h = largeImage.naturalHeight;   
  
     window.open(url,"Image", "height="+ h +", width="+ w +", resizable=yes");
  
     var myWindow = window.self;
  
     myWindow.addEventListener("click", clickHandler);
  
     var elementIsClicked = false;
  
     function clickHandler(){ 
      elementIsClicked = true
     }
  
     function isElementClicked (){
      if(elementIsClicked){
              newWindow.close();
      }
     }
  
     setInterval(isElementClicked, 500);
  
  
  }
  </code></pre>