0

At some point I change an iframe src in order to download a file:

document.getElementById('download_frame').src = "file.zip";

I need some kind of callback to execute when the iframe has completely reloaded (in this case when the file is completely downloaded). I tried:

<iframe id="download_frame" style="display: none;" onload="console.log('load1');">
    <script>
        document.addEventListener("DOMContentLoaded", function(event) {
            console.log('load2');
        });
    </script>
</iframe>

but they are never fired...

Any suggestion?

edoedoedo
  • 1,469
  • 2
  • 21
  • 32

1 Answers1

0

<iframe> tag does not accept a body as scripting (body is alternative content).

You cannot control when a certain file has been loaded because the target of that iframe gets your file thus no any other headers can be sent.

You can try different solutions achieving what you want, for example: Detect when browser receives file download

I did not mark this question as duplicate because it lacks the explanation for iframe scripting.

Community
  • 1
  • 1
Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107