Skip to content

Instantly share code, notes, and snippets.

@dmitry
Created June 10, 2009 12:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmitry/127178 to your computer and use it in GitHub Desktop.
Save dmitry/127178 to your computer and use it in GitHub Desktop.
jQuery.fn.extend({
// upload file through iframe, without refreshing the page, must be wrapped by form
// jquery.timers dependent
// $("input[type='file']").iframeFileUpload({blank_pathname: '/my_blank_file.html', autoSubmit: true, onStart: function(el){}, onStop(el){}});
iframeFileUpload: function(options) {
return this.each(function(){
var blank_pathname = options.blank_pathname || '/blank.html';
var el = $(this);
var form = el.parents('form');
var iframe_id = 'iframe_' + form.attr('id');
if ($('#' + iframe_id).length == 0) {
var iframe = $("<iframe name='" + iframe_id + "'></iframe>"); // holly ie hack
iframe.attr('id', iframe_id);
iframe.css('display', 'none');
iframe.attr('src', blank_pathname);
$('body').prepend(iframe);
}
form.attr('target', iframe_id);
if (options.autoSubmit) {
el.change(function(e){
form.submit();
(options.onStart)(el);
jQuery.active++;
jQuery.event.trigger("ajaxStart");
jQuery.event.trigger("ajaxSend");
$(document).everyTime(100, iframe_id, function(i) {
if ($('#' + iframe_id).contents()[0].location.pathname != blank_pathname) {
$(document).stopTime(iframe_id);
--jQuery.active;
jQuery.event.trigger("ajaxComplete");
jQuery.event.trigger("ajaxStop");
iframe.attr('src', blank_pathname);
$(document).oneTime(10, function(i) {
(options.onStop)(el);
});
}
});
});
}
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment