├── .gitignore ├── Makefile ├── package.json ├── LICENSE.txt ├── README.md ├── jquery-download.min.js ├── jquery-download.js └── demo.html /.gitignore: -------------------------------------------------------------------------------- 1 | DS_Store -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | all: 2 | uglifyjs jquery-download.js > jquery-download.min.js 3 | 4 | clean: 5 | rm jquery-download.min.js 6 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "jquery-download", 3 | "version": "0.1.1", 4 | "title": "jQuery-download", 5 | "author": { 6 | "name": "John Krauss", 7 | "url": "https://github.com/talos" 8 | }, 9 | "licenses": [ 10 | { 11 | "type": "BSD", 12 | "url": "LICENSE.txt" 13 | } 14 | ], 15 | "dependencies": { 16 | "jquery": "1" 17 | }, 18 | "description": "This plugin uses the data: uri to allow users to download arbitrary pieces of DOM, including SVGs.", 19 | "keywords": [ 20 | "download", 21 | "data" 22 | ], 23 | "homepage": "https://github.com/talos/jquery-download", 24 | "maintainers": [ 25 | { 26 | "name": "John Krauss", 27 | "url": "https://github.com/talos" 28 | } 29 | ], 30 | "files": [ 31 | "jquery-download.js", 32 | "jquery-download.min.js" 33 | ] 34 | } -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2012 John Krauss. All rights reserved. 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions 5 | are met: 6 | 7 | 1. Redistributions of source code must retain the above copyright 8 | notice, this list of conditions and the following disclaimer. 9 | 10 | 2. Redistributions in binary form must reproduce the above 11 | copyright notice, this list of conditions and the following 12 | disclaimer in the documentation and/or other materials provided 13 | with the distribution. 14 | 15 | THIS SOFTWARE IS PROVIDED BY JOHN KRAUSS ''AS IS'' AND ANY EXPRESS 16 | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 17 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 | ARE DISCLAIMED. IN NO EVENT SHALL JOHN KRAUSS OR CONTRIBUTORS BE 19 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 21 | OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 22 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 23 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 25 | USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 26 | DAMAGE. 27 | 28 | The views and conclusions contained in the software and 29 | documentation are those of the authors and should not be 30 | interpreted as representing official policies, either expressed or 31 | implied, of John Krauss. 32 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jquery-download 2 | 3 | This jQuery plugin uses the `data:` URI to download the DOM of 4 | arbitrary elements into files. One file will be downloaded for each 5 | selected element. Since the DOM is captured, any javascript-generated 6 | content will be included in the download. 7 | 8 | [See it in action.](http://talos.github.com/jquery-download/demo.html) 9 | 10 | ### Usage 11 | 12 | `failCallback`: (Optional) The browser may not support the `data:` URI. 13 | If this is the case, this function will be called once with the text 14 | of each element not downloaded. 15 | 16 | ```javascript 17 | $(selector).download(failCallback); 18 | ``` 19 | 20 | There is also a utility function you can use to check whether the 21 | browser supports the data URI. This will return `true` if there is 22 | support, and `false` otherwise. 23 | 24 | ```javascript 25 | $().download('support'); 26 | ``` 27 | 28 | ### Examples 29 | 30 | This will download into a browser-named file the contents of the 31 | current page view: 32 | 33 | ```javascript 34 | $('html').download(); 35 | ``` 36 | 37 | This would download every svg on the page into a separate file: 38 | 39 | ```javascript 40 | $('svg').download(); 41 | ``` 42 | 43 | This takes advantage of `failCallback` in case the browser doesn't 44 | support the `data:` uri. 45 | 46 | ```javascript 47 | var failCallback = function(text) { 48 | alert("Could not download " + text); 49 | }; 50 | $('#elem').download(failCallback); 51 | ``` 52 | 53 | ### Limitations 54 | 55 | It is not possible to specify a name for the downloaded file. If at 56 | some point arbitrary headers are added to the `data:` uri spec, this 57 | would be possible using `Content-Disposition` with `filename`. 58 | 59 | There was some [discussion][] about this on the W3.org, but it has not 60 | led to any change in the spec. 61 | 62 | [discussion]: http://lists.w3.org/Archives/Public/uri/2010Feb/0058.html 63 | 64 | Any browsers with limited `data:` uri support will not work, either. 65 | The failCallback is your friend. 66 | 67 | ### Links 68 | 69 | Fork it from 70 | 71 | http://www.github.com/talos/jquery-download 72 | 73 | CDN it at 74 | 75 | http://talos.github.com/jquery-download/jquery-download.js 76 | 77 | http://talos.github.com/jquery-download/jquery-download.min.js 78 | -------------------------------------------------------------------------------- /jquery-download.min.js: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright 2012 John Krauss. All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions 6 | are met: 7 | 8 | 1. Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | 2. Redistributions in binary form must reproduce the above 12 | copyright notice, this list of conditions and the following 13 | disclaimer in the documentation and/or other materials provided 14 | with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY JOHN KRAUSS ''AS IS'' AND ANY EXPRESS 17 | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | ARE DISCLAIMED. IN NO EVENT SHALL JOHN KRAUSS OR CONTRIBUTORS BE 20 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 22 | OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 23 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 24 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 26 | USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 27 | DAMAGE. 28 | 29 | The views and conclusions contained in the software and 30 | documentation are those of the authors and should not be 31 | interpreted as representing official policies, either expressed or 32 | implied, of John Krauss. 33 | **//*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(""),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("
"),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-download.js: -------------------------------------------------------------------------------- 1 | /** 2 | Copyright 2012 John Krauss. All rights reserved. 3 | 4 | Redistribution and use in source and binary forms, with or without 5 | modification, are permitted provided that the following conditions 6 | are met: 7 | 8 | 1. Redistributions of source code must retain the above copyright 9 | notice, this list of conditions and the following disclaimer. 10 | 11 | 2. Redistributions in binary form must reproduce the above 12 | copyright notice, this list of conditions and the following 13 | disclaimer in the documentation and/or other materials provided 14 | with the distribution. 15 | 16 | THIS SOFTWARE IS PROVIDED BY JOHN KRAUSS ''AS IS'' AND ANY EXPRESS 17 | OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19 | ARE DISCLAIMED. IN NO EVENT SHALL JOHN KRAUSS OR CONTRIBUTORS BE 20 | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 22 | OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 23 | BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 24 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE 26 | USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 27 | DAMAGE. 28 | 29 | The views and conclusions contained in the software and 30 | documentation are those of the authors and should not be 31 | interpreted as representing official policies, either expressed or 32 | implied, of John Krauss. 33 | **/ 34 | 35 | /*global define, jQuery, window*/ 36 | 37 | (function (factory) { 38 | "use strict"; 39 | if (typeof define === 'function' && define.amd) { 40 | // AMD. Register as an anonymous module. 41 | define(['jquery'], factory); 42 | } else { 43 | // Browser globals 44 | factory(jQuery); 45 | } 46 | }(function ($) { 47 | "use strict"; 48 | /** 49 | Download some text to the client computer. Only works in 50 | browsers that support the 'data:' scheme. 51 | 52 | @param text The text to download. 53 | **/ 54 | var download = function (text) { 55 | window.location.href = 56 | 'data:application/x-download;charset=utf-8,' + 57 | encodeURIComponent(text); 58 | }, 59 | 60 | // Test to see if data URI is supported 61 | // Thanks to http://weston.ruter.net/2009/05/07/detecting-support-for-data-uris 62 | testImg = $(''), 63 | supported = false, 64 | 65 | isWorking = function (evt) { 66 | supported = (this.width === 1 && this.height === 1) ? true : false; 67 | }; 68 | 69 | testImg.bind('load', isWorking) 70 | .bind('error', isWorking) 71 | .attr('src', "data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///ywAAAAAAQABAAACAUwAOw=="); 72 | 73 | /** 74 | Download the HTML of selected elements as text to the client 75 | computer, one file per element. Only works in browsers that 76 | support the 'data:' scheme. 77 | **/ 78 | $.fn.download = function (arg) { 79 | if (arg === 'support') { 80 | return supported; 81 | } else { 82 | var texts = [], 83 | interval; 84 | 85 | $.each(this, function (i, el) { 86 | var $el = $(el), 87 | $container = $('
'), 88 | text; 89 | 90 | $el.clone().appendTo($container); 91 | text = $container.html(); 92 | 93 | if (supported === true) { 94 | texts.push(text); 95 | } else { 96 | // If user supplied failCallback, call it. 97 | if ($.isFunction(arg)) { 98 | arg(text); 99 | } 100 | } 101 | }); 102 | 103 | // Interval to shuffle through window.location.href 104 | interval = setInterval(function () { 105 | if (texts.length > 0) { 106 | download(texts.pop()); 107 | } else { 108 | clearInterval(interval); 109 | } 110 | }, 100); 111 | 112 | return this; 113 | } 114 | }; 115 | })); -------------------------------------------------------------------------------- /demo.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | jquery-download Demo 10 | 11 | 33 | 34 | 35 | 36 |
37 | 38 | 39 | 40 |
41 |
42 |
43 |
44 | This text was sitting here the whole time. 45 |
46 | 47 | 78 | 79 | 98 | 99 | 100 | --------------------------------------------------------------------------------