63
|
1 /*global jQuery */
|
|
2 /*jslint white: true, browser: true, onevar: true, undef: true, nomen: true, eqeqeq: true, bitwise: true, regexp: true, newcap: true, strict: true */
|
|
3 /**
|
|
4 * jQuery plugin for posting form including file inputs.
|
|
5 *
|
|
6 * Copyright (c) 2010 Ewen Elder
|
|
7 *
|
|
8 * Licensed under the MIT and GPL licenses:
|
|
9 * http://www.opensource.org/licenses/mit-license.php
|
|
10 * http://www.gnu.org/licenses/gpl.html
|
|
11 *
|
|
12 * @author: Ewen Elder <glomainn at yahoo dot co dot uk> <ewen at jainaewen dot com>
|
|
13 * @version: 1.0.1 (2010-07-22)
|
|
14 **/
|
|
15
|
|
16 'use strict';
|
|
17 (function ($)
|
|
18 {
|
|
19 $.fn.iframePostForm = function (options)
|
|
20 {
|
|
21 var contents, elements, element, iframe;
|
|
22
|
|
23 elements = $(this);
|
|
24 options = $.extend({}, $.fn.iframePostForm.defaults, options);
|
|
25
|
|
26 // Add the iframe.
|
|
27 if (!$('#' + options.iframeID).length)
|
|
28 {
|
|
29 $('body').append('<iframe name="' + options.iframeID + '" id="' + options.iframeID + '" style="display:none"></iframe>');
|
|
30 }
|
|
31
|
|
32
|
|
33 return elements.each
|
|
34 (
|
|
35 function ()
|
|
36 {
|
|
37 element = $(this);
|
|
38
|
|
39
|
|
40 // Target the iframe.
|
|
41 element.attr('target', options.iframeID);
|
|
42
|
|
43
|
|
44 // Submit listener.
|
|
45 element.submit
|
|
46 (
|
|
47 function ()
|
|
48 {
|
|
49 options.post.apply(this);
|
|
50
|
|
51 iframe = $('#' + options.iframeID);
|
|
52 iframe.one
|
|
53 (
|
|
54 'load',
|
|
55 function ()
|
|
56 {
|
|
57 contents = iframe.contents().find('body');
|
|
58 options.complete.apply(this, [contents.html()]);
|
|
59
|
|
60 setTimeout
|
|
61 (
|
|
62 function ()
|
|
63 {
|
|
64 contents.html('');
|
|
65 },
|
|
66 1
|
|
67 );
|
|
68 }
|
|
69 );
|
|
70 }
|
|
71 );
|
|
72 }
|
|
73 );
|
|
74 };
|
|
75
|
|
76
|
|
77 $.fn.iframePostForm.defaults = {
|
|
78 iframeID : 'iframe-post-form', // IFrame ID.
|
|
79 post : function () {}, // Form onsubmit.
|
|
80 complete : function (response) {} // After everything is completed.
|
|
81 };
|
|
82 })(jQuery); |