Last active
March 16, 2018 03:12
-
-
Save jdavidbakr/03c0e127c6c3c95d5261 to your computer and use it in GitHub Desktop.
JavaScript: AjaxForm
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Overrides the browser's form submission and sends the form via ajax instead. | |
* Uses the microajax function: https://code.google.com/p/microajax/ | |
*/ | |
var AjaxForm = { | |
iframe: null, | |
form: null, | |
submit: function(form) { | |
if(PageChangeWarning) { | |
PageChangeWarning.saved(); | |
} | |
this.form = form; | |
var has_file = false; | |
var inputs = form.getElementsByTagName('input'); | |
for (var i = 0, n = inputs.length; i < n; i++) | |
{ | |
if (inputs[i].getAttribute('type') == 'file') | |
{ | |
has_file = true; | |
} | |
} | |
if(has_file) { | |
this.submit_with_file(form); | |
} else { | |
this.submit_with_ajax(form); | |
} | |
}, | |
submit_with_ajax: function(form) { | |
var url = form.getAttribute('action'); | |
var data = AjaxForm.serialize(form); | |
form.classList.add('hidden'); | |
microAjax(url, this.form_response.bind(this), data); | |
}, | |
submit_with_file: function(form) { | |
var id = 'iframe_' + Math.random(); | |
var iframe = document.createElement('iframe'); | |
iframe.setAttribute('name',id); | |
iframe.setAttribute('style','display: none'); | |
document.body.appendChild(iframe); | |
form.setAttribute('method','post'); | |
form.setAttribute('enctype','multipart/form-data'); | |
form.setAttribute('target',id); | |
this.iframe = iframe; | |
iframe.addEventListener('load',this.form_response.bind(this)); | |
this.form.submit(); | |
}, | |
form_response: function(json) { | |
if(this.iframe) { | |
var content = this.iframe.contentWindow.document.body.innerHTML; | |
// Strip anything that's not JSON | |
var json_matches = content.match(/[^{]*([^}]*})/); | |
if (json_matches[0]) { | |
json = json_matches[1]; | |
} | |
document.body.removeChild(this.iframe); | |
this.iframe = null; | |
} | |
if(json) { | |
if(JsonProcessor.run(json)) { | |
this.form.classList.remove('hidden'); | |
} | |
} | |
}, | |
submit_button_click: function(form_id) { | |
var form = document.getElementById(form_id); | |
this.submit(form); | |
}, | |
serialize: function(form) { | |
if (!form || !form.elements) | |
return; | |
var serial = [], i, j, first; | |
var add = function (name, value) { | |
serial.push(encodeURIComponent(name) + '=' + encodeURIComponent(value)); | |
}; | |
var elems = form.elements; | |
for (i = 0; i < elems.length; i += 1, first = false) { | |
if (elems[i].name.length > 0) { /* don't include unnamed elements */ | |
switch (elems[i].type) { | |
case 'select-one': | |
first = true; | |
case 'select-multiple': | |
for (j = 0; j < elems[i].options.length; j += 1) | |
if (elems[i].options[j].selected) { | |
add(elems[i].name, elems[i].options[j].value); | |
if (first) | |
break; /* stop searching for select-one */ | |
} | |
break; | |
case 'checkbox': | |
case 'radio': | |
if (!elems[i].checked) | |
break; /* else continue */ | |
default: | |
add(elems[i].name, elems[i].value); | |
break; | |
} | |
} | |
} | |
return serial.join('&'); | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment