0

I am opening a youtube video on popup using javascript. I am using the following code

  var myWindow;
    $("#nextview").click(function() {

    var vidurl = 'www.youtube.com'; 
    myWindow = window.open(vidurl, "popupWindow", "width=600, height=400, scrollbars=yes");
    //counter(myWindow,refreshIntervalId);

});

Now I want to show message when user close this popup window.

What I Tried

myWindow.addEventListener("beforeunload", function(event) {
    event.returnValue = "Write something clever here..";
});

Getting error

Uncaught TypeError: Cannot read property 'addEventListener' of undefined
Azad Chouhan
  • 270
  • 1
  • 5
  • 20

1 Answers1

0

try this

var myWindow;
    $("#nextview").click(function() {

    var vidurl = 'www.youtube.com'; 
    myWindow = window.open(vidurl, "popupWindow", "width=600, height=400, scrollbars=yes");
    //counter(myWindow,refreshIntervalId);
    myWindow.onbeforeunload = function(){ 
       //some code like
       alert("closed");
}
});

this also can able to catch the window close.

Sathish Kumar D
  • 274
  • 6
  • 20