From cc8ebce8acbb54febb3d24c7a77118c1501a922f Mon Sep 17 00:00:00 2001 From: Piotr Sarnacki Date: Mon, 30 Jun 2008 02:59:31 +0200 Subject: [PATCH] Safari support (thanks to Michele Finotto for idea) Safari can't send ajax requests while submitting form, so we must create hidden iframe and send ajax requests from inside of it. --- jquery.uploadProgress.js | 37 ++++++++++++++++++++++++++++++++----- 1 file changed, 32 insertions(+), 5 deletions(-) diff --git a/jquery.uploadProgress.js b/jquery.uploadProgress.js index 689b3bf..bdd18be 100644 --- a/jquery.uploadProgress.js +++ b/jquery.uploadProgress.js @@ -7,8 +7,33 @@ * http://www.opensource.org/licenses/mit-license.php * */ - (function($) { + if($.browser.safari && $('iframe[name=progressFrame]', parent.document).length == 0) { + $(function() { + iframe = document.createElement('iframe'); + iframe.name = "progressFrame"; + $(iframe).css({width: '0', height: '0', position: 'absolute', top: -3000}); + document.body.appendChild(iframe); + + var d = iframe.contentWindow.document; + d.open(); + /* weird - safari won't load scripts without this lines... */ + d.write(''); + d.close(); + + var b = d.body; + var s = d.createElement('script'); + s.src = "../lib/jquery.js"; + /* must be sure that jquery is loaded */ + s.onload = function() { + var s1 = d.createElement('script'); + s1.src = "../jquery.uploadProgress.js"; + b.appendChild(s1); + } + b.appendChild(s); + }); + } + $.fn.uploadProgress = function(options) { return this.each(function(){ $(this).bind('submit', function() { @@ -37,8 +62,8 @@ } else { $(this).attr("action", jQuery(this).attr("action") + "?X-Progress-ID=" + uuid); } - - options.timer = window.setInterval(function() { $.uploadProgress(this, options) }, options.interval); + var uploadProgress = $.browser.safari ? progressFrame.jQuery.uploadProgress : jQuery.uploadProgress; + options.timer = window.setInterval(function() { uploadProgress(this, options) }, options.interval); }); }); }; @@ -56,7 +81,9 @@ jQuery.uploadProgress = function(e, options) { upload = $.extend({ percents: Math.floor((upload.received / upload.size)*1000)/10 }, upload); - $(options.progressBar).width(Math.floor(upload.percents) + '%'); + + bar = $.browser.safari ? $(options.progressBar, parent.document) : $(options.progressBar); + bar.width(Math.floor(upload.percents) + '%'); options.uploading(upload); } /* we are done, stop the interval */ @@ -68,4 +95,4 @@ jQuery.uploadProgress = function(e, options) { }); }; -})(jQuery); \ No newline at end of file +})(jQuery);