-1

I want to know hot to detect popup close event, When i open popup close event automatically call, its i am using .close event.

function start() {
    $.ajax({
        type: "post",
        dataType: "html",
        url: 'client_get',
        crossDomain: true,
        data: {
            'id': '123'
        },
        success: function(response) {
            try {
                var json = JSON.parse(response);
            } catch (err) {
                start();
            }
            jQuery(document).ready(function($) {
                var close_interval = setInterval(function() {
                    var newwindow = window.open('https://www.example.com/key?v=' + json[0]['url'], 'key', 'width=800,height=600,status=0,toolbar=0');
                    if (newwindow.close) {
                        console.log("Closed");
                        clearInterval(close_interval);
                    }
                }, 1000);
            });
        }
    });
}
saulotoledo
  • 1,737
  • 3
  • 19
  • 36
davukocofe
  • 11
  • 4
  • Possible duplicate of [Capture the close event of popup window in JavaScript](https://stackoverflow.com/questions/9388380/capture-the-close-event-of-popup-window-in-javascript) – sebpiq May 27 '18 at 10:24
  • #sebpiq i was try this code also not working for me.. – davukocofe May 27 '18 at 10:25
  • I would start by testing things without the ajax call, so you can isolate the problem better. Clean your code a bit, try only the popup part. – sebpiq May 27 '18 at 10:29
  • Yes i checked changing url is not mistake in single url its also show closed... .this .close code is not working – davukocofe May 27 '18 at 10:37
  • I try without pop its working, there is any way to do same with popup window? – davukocofe May 27 '18 at 10:47

1 Answers1

0

Capturing the close event of a window requires the onbeforeunload event handler:

var new_window = window.open('some url') new_window.onbeforeunload = function(){ my code}

so in your case:

function start() {
$.ajax({
    type: "post",
    dataType: "html",
    url: 'client_get',
    crossDomain: true,
    data: {
        'id': '123'
    },
    success: function (response) {
        try {
            var json = JSON.parse(response);
        } catch (err) {
            start();
        }
        jQuery(document).ready(function ($) {
            var close_interval = setInterval(function () {
                var newwindow = window.open('https://www.example.com/key?v=' + json[0]['url'], 'key', 'width=800,height=600,status=0,toolbar=0');
                newwindow.onload = function() {
                    newwindow.onbeforeunload = function() {
                        console.log("Closed");
                        clearInterval(close_interval);
                    }
                }
            }, 1000);
        });

    }
});
}
Adrian Baginski
  • 336
  • 1
  • 8