Skip to content

Instantly share code, notes, and snippets.

@ericakfranz
Last active December 7, 2015 22:28
Show Gist options
  • Save ericakfranz/c26b087b74ab9c633e69 to your computer and use it in GitHub Desktop.
Save ericakfranz/c26b087b74ab9c633e69 to your computer and use it in GitHub Desktop.
Trigger file download once a successfully submitted optin is closed.
// Function SaveToDisk forces file download instead of viewing in browser
function SaveToDisk(fileURL, fileName) {
// for non-IE
if (!window.ActiveXObject) {
var save = document.createElement('a');
save.href = fileURL;
save.target = '_blank';
save.download = fileName || 'unknown';
var event = document.createEvent('Event');
event.initEvent('click', true, true);
save.dispatchEvent(event);
(window.URL || window.webkitURL).revokeObjectURL(save.href);
}
// for IE
else if ( !! window.ActiveXObject && document.execCommand) {
var _window = window.open(fileURL, '_blank');
_window.document.close();
_window.document.execCommand('SaveAs', true, fileName || fileURL)
_window.close();
}
}
jQuery(document).ready(function($){
// Specify the OptinMonster Event that will close our optin
$(document).on('OptinMonsterOptinSuccess', function(event, data, object){
object.close();
});
// Specify the OptinMonster Event that will trigger our file download
$(document).on('OptinMonsterOptinSuccessClose', function(event, data, object){
// Update with the url of the file you want downloaded on optin submission
SaveToDisk('http://www.example.com/download-file.pdf');
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment