1

I have a php page to create an excel file and save it to a folder. I start from a page where user click on a button to start the script. My goal is to perform the following steps:

  1. run the script in another window (I am now using a small popup);
  2. when the script ends close the popup and in the main window display an alert to say that execution is ok;
  3. on alert close refresh the file list displayed in the main window.

I know how to do all these stuffs but one: how can i trigger any event (popup close and alert) when in the popup the script ends?

My actual code for the popup is the following:

$('#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.
    }
});

The script (beginning and end only)

<?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>";
?>
Lelio Faieta
  • 5,913
  • 6
  • 34
  • 57

2 Answers2

0

Have you tried with onbeforeunload?

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

Edit: Just add this event to your opened window like this:

$('#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
        }
    }
});

Edit: You can close the window from your script. In fact browser doesn't allow to close the current opened window. You should call the close function from the caller but here is a little trick to close it anyway, use this function:

function close_popupWindow() {
    window.self.opener = window.self;
    window.self.close();
}
Ludovic Feltz
  • 10,007
  • 3
  • 43
  • 51
0

If you want to do something in the new window when the script ends, use onbeforeunload as Ludvoic suggests.

If, on the other hand, you want to do something in the parent page after the child page is done running its script, that is a different story. You imply this in the question and therefore will answer this scenario.

You can use localstorage provided that both pages are served from the same domain. When the script is done running, the child page can update a value. The parent page can listen to the storage event to know when that happens. Here is a post discussing how to share data across tabs:

Javascript; communication between tabs/windows with same origin

In the parent page:

$('#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
    }
 });

In the popup:

// Do stuff
localStorage.popup_script_done = true;
Community
  • 1
  • 1
Vytas Bradunas
  • 586
  • 1
  • 6
  • 15