Created
April 17, 2020 17:55
-
-
Save imarkdesigns/4f2f98acfe2ce6612ace9393a5173a05 to your computer and use it in GitHub Desktop.
Force Download via jQuery #jquery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var downloadTrigger = getUrlParameter('download'); | |
| if ( downloadTrigger == 'true' ) { | |
| function download_file(fileURL, fileName) { | |
| // for non-IE | |
| if (!window.ActiveXObject) { | |
| var save = document.createElement('a'); | |
| save.href = fileURL; | |
| save.target = '_blank'; | |
| var filename = fileURL.substring(fileURL.lastIndexOf('/')+1); | |
| save.download = fileName || filename; | |
| if ( navigator.userAgent.toLowerCase().match(/(ipad|iphone|safari)/) && navigator.userAgent.search("Chrome") < 0) { | |
| document.location = save.href; | |
| // window event not working here | |
| } else { | |
| var evt = new MouseEvent('click', { | |
| 'view': window, | |
| 'bubbles': true, | |
| 'cancelable': false | |
| }); | |
| save.dispatchEvent(evt); | |
| (window.URL || window.webkitURL).revokeObjectURL(save.href); | |
| } | |
| } | |
| // for IE < 11 | |
| else if ( !! window.ActiveXObject && document.execCommand) { | |
| var _window = window.open(fileURL, '_blank'); | |
| _window.document.close(); | |
| _window.document.execCommand('SaveAs', true, fileName || fileURL) | |
| _window.close(); | |
| } | |
| } | |
| download_file("myPDFDownload.pdf", "Download.pdf"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment