javascript - close window after download file generates -
i have javascript
code generate pdf file. have url
generates pdf file. dont want user see url thats why using iframe in hidden load url , generate download popup. working me, want open iframe content in new tab, wait till download popup generate , close window.
see javascript,
var iframe; var url="test.com?file=download"; iframe="<iframe src='"+url+"' style='visibility:hidden' >"; var html="<html>"; html+="<head>"; html+="</head>"; html+="<body>"; html+= iframe; html+="</body>"; html+="</html>"; var printwin = window.open('','_blank','left=0,top=0,fullscreen,toolbar=0,scrollbars=1,status=0'); printwin.document.write(html); printwin.document.close(); printwin.focus();
if using printwin.close();
sudden close window without download file. there solution this?
if may suggest alternative solution without using hidden iframes , opening / closing tabs. in html5 can use attribute on link called download
. sample code using purely js be:
// select download button var btn = document.getelementbyid('download-btn-id'); // url pdf, // result of code have running in iframe. btn.href = url; // name file , add .pdf extension. btn.download = 'filename.pdf';
when user clicks on button, pdf downloaded without leaving page.
if need trigger download @ runtime, can add line acts if user clicked on download button, result pdf downloaded when run function.
btn.click();
Comments
Post a Comment