弹出关闭事件检测javascript

时间:2018-05-27 10:21:00

标签: javascript jquery

我想知道热点检测弹出关闭事件,当我打开弹出关闭事件自动调用时,我正在使用.close事件。

    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);         
       });
        } 
      }); 
} 

1 个答案:

答案 0 :(得分:0)

捕获窗口的关闭事件需要onbeforeunload事件处理程序:

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

所以在你的情况下:

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);
        });

    }
});
}