Created a file download script that redirects the user to the file processing page and immediately after it downloads it by displaying a dialog box.

The question is how to determine that the file download has started or the dialog box is closed or something to start the transition to the main page?

So I run the file download:

<script>location.replace("/download.php?file='.$name.'");</script> 

And in download.php everything is standard taken from here .

    1 answer 1

    You can open the file download in a separate window / tab, and check when it will be closed.

    Example:

     <script type='text/javascript'> function openTab(url, callback){ var popup = window.open(url, "_blank"); if(typeof callback == 'function'){ var interval = window.setInterval(function() { try { if (popup == null || popup.closed) { window.clearInterval(interval); callback(); } } catch (e) { } }, 500); } popup.focus(); } //JS document.getElementById('link-download').onclick = function (event){ openTab(event.target.href, function(){ /* Действия после старта скачивания */ window.location.reload(); }); return false; } //jQuery $(document).on('click', '.download-file', function(e){ e.preventDefault(); openTab(event.target.href, function(){ /* Действия после старта скачивания */ window.location.reload(); }); }); </script> <a href="/download.php?file='<?php echo $name;?>'" id="link-download" class="download-file">Скачать</a> 

    If there are problems in the download.php file, or the file is not found, and you want to close a new window / tab, you can simply output:

     echo "<script type='text/javascript' charset='utf-8'>if(window.opener){window.close();}</script>"; 

    PS Use one of the options either with the jQuery framework or on pure js , do not leave both.

    PSS Example, for example, copy-paste may not work.

    PSSS The tracking method for closing the window may not work.

    • In general, I understand it is impossible to track? For this option is really not very reliable ... - Telion