Js asynchronous form submission, image upload, compatible with asynchronous ajax simulation technology

Source: Internet
Author: User
Tags tagname


Var asyn = {
FormSubmit: function (args, action, func ){
This. clearContext ();
This. callBack = null;
Var subArr = [];
Var subArrT = [];
If (B $. type. isArray (args )){
SubArr = args;
} Else {
Var tag = args. tagName. toLowerCase ();
If (tag = "form") {for (var I = 0, num = args. childNodes. length; I <num; I ++) {subArr. push (args. childNodes [I]) ;}}
Else {subArr = [args];}
}
// Create asyn form and ifroma
Var objForm = document. createElement ("form ");
ObjForm. action = action;
ObjForm.tar get = "bBankAsynFormSubmit_iframe_1b ";
ObjForm. encoding = "multipart/form-data ";
ObjForm. method = "post ";
ObjForm. id = "bBankAsynFormSubmit_form_1b ";
ObjForm. style. display = "none ";
Var objIframe = B $. parseDom ('<iframe id = "bBankAsynFormSubmit_iframe_1b" name = "bBankAsynFormSubmit_iframe_1b" src = "about: blank" style = "display: none;" onload = "javascript tutorial: setTimeout ('asyn. complete () ', 100) "> </iframe> ') [0];
// Add submit value in form
For (var I = 0, num = subArr. length; I <num; I ++ ){
If (! SubArr [I]. name & subArr [I]. nodeType = 1 & subArr [I]. tagName. toLowerCase () = "input") subArr [I]. name = "bBankAsynFormSubmit_input_1b _" + I;
Var input = subArr [I]. cloneNode (true );
SubArrT. push (input );
SubArr [I]. parentNode. replaceChild (input, subArr [I]);
ObjForm. appendChild (subArr [I]);
}
// Submit
Document. body. appendChild (objIframe );
Document. body. appendChild (objForm );
ObjForm. submit ();
// Dispose
For (var I = 0, num = subArrT. length; I <num; I ++) {subArrT [I]. parentNode. replaceChild (subArr [I], subArrT [I]);}
If (func) this. callBack = func;
},
Complete: function (){
Var responseText = "";
Try {
Var objIframe = document. getElementById ("bBankAsynFormSubmit_iframe_1b ");
If (objIframe. contentWindow) {responseText = objiframe.content20.doc ument. body. innerHTML ;}
Else {responseText = objIframe.contentDocument.doc ument. body. innerHTML ;}
} Catch (err ){}
This. clearContext ();
If (this. callBack) this. callBack (responseText );
},
ClearContext: function (){
If (B $ ('# bBankAsynFormSubmit_form_1b') B $ ('# bBankAsynFormSubmit_form_1b'). removeSelf ();
If (B $ ('# bBankAsynFormSubmit_iframe_1b') B $ ('# bBankAsynFormSubmit_iframe_1b'). removeSelf ();
},
CallBack: null
};

Integration method


B $. asyn. formSubmit (inputArr, 'XXX. asp tutorial X', function (d ){
Alert (d );
});

The core is to use iframe to complete asynchronization. Put a hidden iframe in the page, and the form has a property target. Set the target as the iframe for asynchronous submission. When you submit the form, in fact, iframe is used as the carrier for submitting the display, and other content on the page is refreshing.
In fact, the principle is very simple. Since iframe is used as the display carrier, we can extend it to the following: if the submitted page has a returned value, the returned value will become the content in the body of iframe, whether the content in the body is obtained through iframe is a bit like ajax. This is the earliest method used to implement asynchronization.
Ajax is time-triggered. To simulate ajax, we also need to trigger the event. In fact, it is very easy to use the iframe onload to load the event.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.