I had to write a javascript function that on-click of a button downloads a pdf file and on-success shows a message. I followed the usual pattern-
const download = (url) -> {
$(`<iframe src=${url} frameborder=0 scrolling="no" id="someid"`)
.load(() -> {
//show the success message
$(this).remove;
}).appendTo('body')
}but ofcourse this didn't work. As it turns out the onload event does not fire unless there the content insiide the iframe changes. In this case, since the iframe gets the pdf data and opens up the save as option, there is no content change and hence no onload event.
Here's how I solved it-
const download = (url) -> {
const pdfCallback = () -> {
const iframe = $("#someid");
const iframedoc = iframe[0].contentWindow.document;
if(iframedoc.readyState == 'complete') {
//show the success message
iframe.remove();
clearInterval(myTimer);
}
}
$(`<iframe src=${url} frameborder=0 scrolling="no" id='someid"`)
.appendTo('body');
var myTimer = setInterval(pdfCallback, 100);
}