Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

amd compliance

  • Loading branch information...
commit aac0f7cbcfb9e7fa4cab849a6a9f7ae36caeaad0 1 parent 9449768
@talos authored
Showing with 40 additions and 23 deletions.
  1. +38 −21 jquery-download.js
  2. +1 −1  jquery-download.min.js
  3. +1 −1  package.json
View
59 jquery-download.js
@@ -31,61 +31,78 @@
interpreted as representing official policies, either expressed or
implied, of John Krauss.
**/
-(function($) {
+
+/*global define, jQuery, window*/
+
+(function (factory) {
+ "use strict";
+ if (typeof define === 'function' && define.amd) {
+ // AMD. Register as an anonymous module.
+ define(['jquery'], factory);
+ } else {
+ // Browser globals
+ factory(jQuery);
+ }
+}(function ($) {
+ "use strict";
/**
Download some text to the client computer. Only works in
browsers that support the 'data:' scheme.
@param text The text to download.
**/
- var download = function(text) {
+ var download = function (text) {
window.location.href =
'data:application/x-download;charset=utf-8,' +
encodeURIComponent(text);
},
- // Test to see if data URI is supported
- // Thanks to http://weston.ruter.net/2009/05/07/detecting-support-for-data-uris
- data = new Image(),
- supported = false;
- data.onload = data.onerror = function() {
- supported = (this.width === 1 && this.height === 1) ? true : false;
- };
- data.src = "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==";
+ // Test to see if data URI is supported
+ // Thanks to http://weston.ruter.net/2009/05/07/detecting-support-for-data-uris
+ testImg = $('<img />'),
+ supported = false,
+
+ isWorking = function (evt) {
+ supported = (this.width === 1 && this.height === 1) ? true : false;
+ };
+
+ testImg.bind('load', isWorking)
+ .bind('error', isWorking)
+ .attr('src', "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==");
/**
Download the HTML of selected elements as text to the client
computer, one file per element. Only works in browsers that
support the 'data:' scheme.
**/
- $.fn.download = function(arg) {
- if(arg === 'support') {
+ $.fn.download = function (arg) {
+ if (arg === 'support') {
return supported;
} else {
var texts = [],
- interval;
+ interval;
- $.each(this, function(i, el) {
+ $.each(this, function (i, el) {
var $el = $(el),
- $container = $('<div />'),
- text;
+ $container = $('<div />'),
+ text;
$el.clone().appendTo($container);
text = $container.html();
- if(supported === true) {
+ if (supported === true) {
texts.push(text);
} else {
// If user supplied failCallback, call it.
- if($.isFunction(arg)) {
+ if ($.isFunction(arg)) {
arg(text);
}
}
});
// Interval to shuffle through window.location.href
- interval = setInterval(function() {
- if(texts.length > 0) {
+ interval = setInterval(function () {
+ if (texts.length > 0) {
download(texts.pop());
} else {
clearInterval(interval);
@@ -95,4 +112,4 @@
return this;
}
};
-})(jQuery);
+}));
View
2  jquery-download.min.js
@@ -30,4 +30,4 @@
documentation are those of the authors and should not be
interpreted as representing official policies, either expressed or
implied, of John Krauss.
-**/(function(a){var b=function(a){window.location.href="data:application/x-download;charset=utf-8,"+encodeURIComponent(a)},c=new Image,d=!1;c.onload=c.onerror=function(){d=this.width===1&&this.height===1?!0:!1},c.src="data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw==",a.fn.download=function(c){if(c==="support")return d;var e=[],f;return a.each(this,function(b,f){var g=a(f),h=a("<div />"),i;g.clone().appendTo(h),i=h.html(),d===!0?e.push(i):a.isFunction(c)&&c(i)}),f=setInterval(function(){e.length>0?b(e.pop()):clearInterval(f)},100),this}})(jQuery);
+**//*global define, jQuery, window*/(function(a){"use strict",typeof define=="function"&&define.amd?define(["jquery"],a):a(jQuery)})(function(a){"use strict";var b=function(a){window.location.href="data:application/x-download;charset=utf-8,"+encodeURIComponent(a)},c=a("<img />"),d=!1,e=function(a){d=this.width===1&&this.height===1?!0:!1};c.bind("load",e).bind("error",e).attr("src","data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=="),a.fn.download=function(c){if(c==="support")return d;var e=[],f;return a.each(this,function(b,f){var g=a(f),h=a("<div />"),i;g.clone().appendTo(h),i=h.html(),d===!0?e.push(i):a.isFunction(c)&&c(i)}),f=setInterval(function(){e.length>0?b(e.pop()):clearInterval(f)},100),this}});
View
2  package.json
@@ -1,6 +1,6 @@
{
"name": "jquery-download",
- "version": "0.1.0",
+ "version": "0.1.1",
"title": "jQuery-download",
"author": {
"name": "John Krauss",

0 comments on commit aac0f7c

Please sign in to comment.
Something went wrong with that request. Please try again.