├── .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("