* Requires jQuery v1.7 or later
* Copyright 2017 Kevin Morris
* Copyright 2006 M. Alsup
* Project repository: https://github.com/jquery-form/form
* Dual licensed under the MIT and LGPLv3 licenses.
* https://github.com/jquery-form/form#license
/* global ActiveXObject */
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(['jquery'], factory);
} else if (typeof module === 'object' && module.exports) {
module.exports = function( root, jQuery ) {
if (typeof jQuery === 'undefined') {
// require('jQuery') returns a factory that requires window to build a jQuery instance, we normalize how we use modules
// that require this pattern but the window provided is a noop if it's defined (how jquery works)
if (typeof window !== 'undefined') {
jQuery = require('jquery');
jQuery = require('jquery')(root);
Do not use both ajaxSubmit and ajaxForm on the same form. These
functions are mutually exclusive. Use ajaxSubmit if you want
to bind your own submit handler to the form. For example,
$(document).ready(function() {
$('#myForm').on('submit', function(e) {
e.preventDefault(); // <-- important
Use ajaxForm when you want the plugin to manage all the event binding
$(document).ready(function() {
You can also use ajaxForm with delegation (requires jQuery v1.7+), so the
form does not have to exist when you invoke ajaxForm:
When using ajaxForm, the ajaxSubmit function will be invoked for you
feature.fileapi = $('<input type="file">').get(0).files !== undefined;
feature.formdata = (typeof window.FormData !== 'undefined');
var hasProp = !!$.fn.prop;
// attr2 uses prop when it can but checks the return type for
// an expected string. This accounts for the case where a form
// contains inputs with names like "action" or "method"; in those
// cases "prop" returns the element
$.fn.attr2 = function() {
return this.attr.apply(this, arguments);
var val = this.prop.apply(this, arguments);
if ((val && val.jquery) || typeof val === 'string') {
return this.attr.apply(this, arguments);
* ajaxSubmit() provides a mechanism for immediately submitting
* an HTML form using AJAX.
* @param {object|string} options jquery.form.js parameters or custom url for submission
* @param {object} data extraData
* @param {string} dataType ajax dataType
* @param {function} onSuccess ajax success callback function
$.fn.ajaxSubmit = function(options, data, dataType, onSuccess) {
// fast fail if nothing selected (http://dev.jquery.com/ticket/2752)
log('ajaxSubmit: skipping submit process - no element selected');
/* eslint consistent-this: ["error", "$form"] */
var method, action, url, $form = this;
if (typeof options === 'function') {
options = {success: options};
} else if (typeof options === 'string' || (options === false && arguments.length > 0)) {
if (typeof onSuccess === 'function') {
options.success = onSuccess;
} else if (typeof options === 'undefined') {
method = options.method || options.type || this.attr2('method');
action = options.url || this.attr2('action');
url = (typeof action === 'string') ? $.trim(action) : '';
url = url || window.location.href || '';
// clean url (don't include hash vaue)
url = (url.match(/^([^#]+)/) || [])[1];
options = $.extend(true, {
success : $.ajaxSettings.success,
type : method || $.ajaxSettings.type,
iframeSrc : /^https/i.test(window.location.href || '') ? 'javascript:false' : 'about:blank' // eslint-disable-line no-script-url
// hook for manipulating the form data before it is extracted;
// convenient for use with rich editors like tinyMCE or FCKEditor
this.trigger('form-pre-serialize', [this, options, veto]);
log('ajaxSubmit: submit vetoed via form-pre-serialize trigger');
// provide opportunity to alter form data before it is serialized
if (options.beforeSerialize && options.beforeSerialize(this, options) === false) {
log('ajaxSubmit: submit aborted via beforeSerialize callback');
var traditional = options.traditional;
if (typeof traditional === 'undefined') {
traditional = $.ajaxSettings.traditional;
var qx, a = this.formToArray(options.semantic, elements, options.filtering);
var optionsData = $.isFunction(options.data) ? options.data(a) : options.data;
options.extraData = optionsData;
qx = $.param(optionsData, traditional);
// give pre-submit callback an opportunity to abort the submit
if (options.beforeSubmit && options.beforeSubmit(a, this, options) === false) {
log('ajaxSubmit: submit aborted via beforeSubmit callback');
// fire vetoable 'validate' event
this.trigger('form-submit-validate', [a, this, options, veto]);
log('ajaxSubmit: submit vetoed via form-submit-validate trigger');
var q = $.param(a, traditional);
q = (q ? (q + '&' + qx) : qx);
if (options.type.toUpperCase() === 'GET') {
options.url += (options.url.indexOf('?') >= 0 ? '&' : '?') + q;
options.data = null; // data is null for 'get'
options.data = q; // data is the query string for 'post'
callbacks.push(function() {
callbacks.push(function() {
$form.clearForm(options.includeHidden);
// perform a load on the target only if dataType is not provided
if (!options.dataType && options.target) {
var oldSuccess = options.success || function(){};
callbacks.push(function(data, textStatus, jqXHR) {
var successArguments = arguments,
fn = options.replaceTarget ? 'replaceWith' : 'html';
$(options.target)[fn](data).each(function(){
oldSuccess.apply(this, successArguments);
} else if (options.success) {
if ($.isArray(options.success)) {
$.merge(callbacks, options.success);
callbacks.push(options.success);
options.success = function(data, status, xhr) { // jQuery 1.4+ passes xhr as 3rd arg
var context = options.context || this; // jQuery 1.4+ supports scope context
for (var i = 0, max = callbacks.length; i < max; i++) {
callbacks[i].apply(context, [data, status, xhr || $form, $form]);
var oldError = options.error;
options.error = function(xhr, status, error) {
var context = options.context || this;
oldError.apply(context, [xhr, status, error, $form]);
var oldComplete = options.complete;
options.complete = function(xhr, status) {
var context = options.context || this;
oldComplete.apply(context, [xhr, status, $form]);
// are there files to upload?
// [value] (issue #113), also see comment:
// https://github.com/malsup/form/commit/588306aedba1de01388032d5f42a60159eea9228#commitcomment-2180219
var fileInputs = $('input[type=file]:enabled', this).filter(function() {
return $(this).val() !== '';
var hasFileInputs = fileInputs.length > 0;
var mp = 'multipart/form-data';
var multipart = ($form.attr('enctype') === mp || $form.attr('encoding') === mp);
var fileAPI = feature.fileapi && feature.formdata;
log('fileAPI :' + fileAPI);
var shouldUseFrame = (hasFileInputs || multipart) && !fileAPI;
// options.iframe allows user to force iframe mode
// 06-NOV-09: now defaulting to iframe mode if file input is detected
if (options.iframe !== false && (options.iframe || shouldUseFrame)) {
// hack to fix Safari hang (thanks to Tim Molendijk for this)
// see: http://groups.google.com/group/jquery-dev/browse_thread/thread/36395b7ab510dd5d
if (options.closeKeepAlive) {
$.get(options.closeKeepAlive, function() {
jqxhr = fileUploadIframe(a);
jqxhr = fileUploadIframe(a);
} else if ((hasFileInputs || multipart) && fileAPI) {
jqxhr = fileUploadXhr(a);
$form.removeData('jqxhr').data('jqxhr', jqxhr);
for (var k = 0; k < elements.length; k++) {
this.trigger('form-submit-notify', [this, options]);
// utility fn for deep serialization
function deepSerialize(extraData) {
var serialized = $.param(extraData, options.traditional).split('&');
var len = serialized.length;
for (i = 0; i < len; i++) {
// #252; undo param space replacement
serialized[i] = serialized[i].replace(/\+/g, ' ');
part = serialized[i].split('=');
// #278; use array instead of object storage, favoring array serializations
result.push([decodeURIComponent(part[0]), decodeURIComponent(part[1])]);
// XMLHttpRequest Level 2 file uploads (big hat tip to francois2metz)
function fileUploadXhr(a) {
var formdata = new FormData();
for (var i = 0; i < a.length; i++) {
formdata.append(a[i].name, a[i].value);
var serializedData = deepSerialize(options.extraData);
for (i = 0; i < serializedData.length; i++) {
formdata.append(serializedData[i][0], serializedData[i][1]);
var s = $.extend(true, {}, $.ajaxSettings, options, {
if (options.uploadProgress) {
// workaround because jqXHR does not expose upload property
var xhr = $.ajaxSettings.xhr();
xhr.upload.addEventListener('progress', function(event) {
var position = event.loaded || event.position; /* event.position is deprecated */
if (event.lengthComputable) {
percent = Math.ceil(position / total * 100);
options.uploadProgress(event, position, total, percent);
var beforeSend = s.beforeSend;
s.beforeSend = function(xhr, o) {
// Send FormData() provided by user
o.data = options.formData;
beforeSend.call(this, xhr, o);
// private function for handling file uploads (hat tip to YAHOO!)
function fileUploadIframe(a) {
var form = $form[0], el, i, s, g, id, $io, io, xhr, sub, n, timedOut, timeoutHandle;
var deferred = $.Deferred();
deferred.abort = function(status) {
// ensure that every serialized input is still enabled
for (i = 0; i < elements.length; i++) {
el.prop('disabled', false);
el.removeAttr('disabled');
s = $.extend(true, {}, $.ajaxSettings, options);
s.context = s.context || s;
id = 'jqFormIO' + new Date().getTime();
var ownerDocument = form.ownerDocument;
var $body = $form.closest('body');
$io = $(s.iframeTarget, ownerDocument);
$io = $('<iframe name="' + id + '" src="' + s.iframeSrc + '" />', ownerDocument);
$io.css({position: 'absolute', top: '-1000px', left: '-1000px'});
getAllResponseHeaders : function() {},
getResponseHeader : function() {},
setRequestHeader : function() {},
abort : function(status) {
var e = (status === 'timeout' ? 'timeout' : 'aborted');
log('aborting upload... ' + e);
if (io.contentWindow.document.execCommand) {
io.contentWindow.document.execCommand('Stop');
$io.attr('src', s.iframeSrc); // abort op in progress
s.error.call(s.context, xhr, e, status);