Created
February 19, 2016 20:30
-
-
Save gowon/13a1ca47e828c754ae05 to your computer and use it in GitHub Desktop.
jQuery-powered AJAX downloader
This file contains 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
// 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