在脚本执行结束时关闭弹出窗口

close popup window on script execution end

本文关键字:窗口 脚本 执行 结束      更新时间:2023-09-26

我有一个php页面来创建excel文件并将其保存到文件夹中。我从一个页面开始,用户单击按钮来启动脚本。我的目标是执行以下步骤:

  1. 在另一个窗口中运行脚本(我现在正在使用一个小弹出窗口(;
  2. 脚本结束时关闭弹出窗口,并在主窗口中显示警报,指出执行正常;
  3. 在警报关闭时,刷新主窗口中显示的文件列表。

知道如何做所有这些事情,但有一个:当弹出窗口中脚本结束时,我如何触发任何事件(弹出窗口关闭和警报(?

我弹出窗口的实际代码如下:

$('#elabora').click(function(e){
    e.preventDefault();
    var data = $('#data_rif').val();
    if (data=='') {
        $("#spnmsg").fadeTo(200,0.1,function(){
            $(this).removeClass().addClass('spn_error').html("Inserisci una data di riferimento per l'elaborazione").fadeTo(900,1);
        });
    }else{
        window.open('sofferenze/trxls.php?data='+data, "popupWindow", "width=10,height=10");
//here is where I want to trigger that when trxls.php ends the alert should be displayed.
    }
});

脚本(仅限开头和结尾(

<?php
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$cacheMethod = PHPExcel_CachedObjectStorageFactory::cache_to_sqlite3;
PHPExcel_Settings::setCacheStorageMethod($cacheMethod);
// Create a new PHPExcel object 
$ea = new PHPExcel();
...
$objWriter = PHPExcel_IOFactory::createWriter($ea, 'Excel2007');
ob_end_clean();
//$objWriter->save('php://output');
$objWriter->save('trackrecord/'.$data.'_trackrecord.xlsx"');
exit();
echo "<script>window.close();</script>";
?>

你试过onbeforeunload吗?

window.onbeforeunload = function (event) {
    // ...
}

编辑:只需将此事件添加到打开的窗口中,如下所示:

$('#elabora').click(function(e){
    e.preventDefault();
    var data = $('#data_rif').val();
    if (data=='') {
        $("#spnmsg").fadeTo(200,0.1,function(){
            $(this).removeClass().addClass('spn_error').html("Inserisci una data di riferimento per l'elaborazione").fadeTo(900,1);
        });
    }else{
        var wndRef = window.open('sofferenze/trxls.php?data='+data, "popupWindow", "width=10,height=10");
        wndRef.onbeforeunload = function (event) {
              // Show your alert
        }
    }
});

编辑:您可以从脚本关闭窗口。事实上,浏览器不允许关闭当前打开的窗口。您应该从调用方调用 close 函数,但这里有一个小技巧可以关闭它,请使用此函数:

function close_popupWindow() {
    window.self.opener = window.self;
    window.self.close();
}
如果你想在

脚本结束时在新窗口中做一些事情,按照Ludvoic的建议使用onbeforeunload。

另一方面,如果您想在子页面运行完其脚本后在父页面中执行某些操作,那就是另一回事了。您在问题中暗示了这一点,因此将回答这种情况。

您可以使用localstorage前提是两个网页都来自同一网域。脚本运行完毕后,子页面可以更新值。父页面可以侦听 storage 事件以了解何时发生。以下是一篇讨论如何在选项卡之间共享数据的帖子:

Javascript;具有相同来源的选项卡/窗口之间的通信

在父页面中:

$('#elabora').click(function(e){
    e.preventDefault();
    var data = $('#data_rif').val();
    if (data=='') {
        $("#spnmsg").fadeTo(200,0.1,function(){
            $(this).removeClass().addClass('spn_error').html("Inserisci una data di riferimento per l'elaborazione").fadeTo(900,1);
        });
    }else{
        localStorage.popup_script_done = false;
        window.open('sofferenze/trxls.php?data='+data, "popupWindow", "width=10,height=10");
    }
});
$(window).bind('storage', function (e) {
    if (localStorage.popup_script_done == true) {
        // Do stuff
    }
 });

在弹出窗口中:

// Do stuff
localStorage.popup_script_done = true;