skip to Main Content
 const newWindow = window.open(
      "",
      "_blank",
      "width=800,height=600,left=2000,top=0"
    );

    if (newWindow) {
      const specificDivContent = document.getElementById(
        `slide-${slideIndex}`
      ).innerHTML;

above is the code i am using to create a new tab and write an existing div onto that tab. i would like to call an api when the new window that is opened is refreshed or closed.
tried using these listeners below.

 window.addEventListener("beforeunload", function(event) {
    // Prevent the user from leaving the page.
    console.log('tried closing the window')
    event.preventDefault();
  });
  
  function checkWindowClosed() {
    if (window.closed) {
      console.log('window closed')
    } else console.log('still open')
  }
  
  setInterval(checkWindowClosed, 1000);

2

Answers


  1. want to add the listener to the newWindow you created using window.open()?

    Login or Signup to reply.
  2. With a little error checking we’ll know if accessing the window is successful…

    try{
    
     newWindow.document.body.appendChild('<div>hello</div>'); // or some other operation
    
    } catch(e){
    
     alert(e);
    
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search