【问题标题】:popup close event detect javascript弹出关闭事件检测javascript
【发布时间】:2018-05-27 10:21:00
【问题描述】:

我想知道热门检测弹出关闭事件,当我打开弹出关闭事件自动调用时,我正在使用.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);
            });
        }
    });
}

【问题讨论】:

  • #sebpiq 我尝试了这段代码也对我不起作用..
  • 我将首先在没有 ajax 调用的情况下进行测试,这样您就可以更好地隔离问题。稍微清理一下代码,只尝试弹出部分。
  • 是的,我检查了更改 url 不是单个 url 中的错误,它也显示关闭...。这个 .close 代码不起作用
  • 我尝试不弹出它的工作,有什么方法可以对弹出窗口做同样的事情吗?

标签: javascript jquery


【解决方案1】:

捕获窗口的关闭事件需要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);
        });

    }
});
}

【讨论】:

    猜你喜欢
    • 2012-10-23
    • 1970-01-01
    • 2012-05-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多