Skip to content

Instantly share code, notes, and snippets.

@gowon
Created February 19, 2016 20:30
Show Gist options
  • Save gowon/13a1ca47e828c754ae05 to your computer and use it in GitHub Desktop.
Save gowon/13a1ca47e828c754ae05 to your computer and use it in GitHub Desktop.
jQuery-powered AJAX downloader
// http://www.adequatelygood.com/JavaScript-Module-Pattern-In-Depth.html
// http://stackoverflow.com/a/20354786
var UTIL = (function (parent, $, undefined) {
var my = parent._private = parent._private || {};
my.iframeform = function (url, callback) {
var object = this;
object.time = new Date().getTime();
object.callback = callback || function (){};
object.form = $('<form action="' + url + '" target="iframe' + object.time + '" method="post" style="display:none;" id="form' + object.time + '"></form>');
object.addParameter = function (parameter, value) {
$("<input type='hidden' />")
.attr("name", parameter)
.attr("value", value)
.appendTo(object.form);
}
object.send = function () {
var iframe = $('<iframe data-time="' + object.time + '" style="display:none;" id="iframe' + object.time + '" name="iframe' + object.time + '"></iframe>');
$("body").append(iframe);
$("body").append(object.form);
object.form.submit();
iframe.on( "load", function (event) {
$('#form' + $(this).data('time')).remove();
$(this).remove();
// execute callback
object.callback(event);
});
}
}
return parent;
} (UTIL || {}, jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment