Javascript asynchronous form submission, Image Upload, and compatibility with asynchronous Ajax Simulation Technology

Source: Internet
Author: User

Preface:
I think the title is still quite long. There are many other functions. In fact, the simplification is just a function. asynchronous form submission only supports Image Upload under the big function of asynchronous form submission, simulate Ajax technology (in fact, it was a long time ago that it was used to implement compatibility with Ajax in multiple browsers. Here it is just a nostalgia for playing as a technology ), the following content must have a certain JS Foundation, otherwise it will be difficult to understand.
Note:
This is a method in bbank. Now I extract it into a general method to explain it.
Bbank framework: http://www.cnblogs.com/bruceli/archive/2010/04/15/bBank.html
Why should we mention bbank, because in the following method, we will use the bbank array judgment, the string HTML conversion standard DOM (my previous article Article This is an introduction. Click here for details) to remove your own methods and CSS selectors. You can replace them with your own methods, you can also use the bbank method to download the bbank file. Source code .
B $. type. isarray (ARGs) determines whether it is an array
B $. parsedom (STRs) string is directly converted to a standard DOM object
B $ ('# bbankasynformsubmit_form_1b'). removeself () Remove yourself
B $ () CSS Selector
Principle:
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.
Implementation:
Code: CopyCode The Code is as follows: 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: 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
};

simple explanation:
In fact, it is troublesome to solve the problem of IFRAME multi-browser compatibility, in fact, we do not need to discuss the compatibility problem of IFRAME. We leave the compatibility to the browser for resolution and solution. Here we use string to Dom.
first, I created a form and IFRAME, both of which are hidden. I transferred the content of the form to the hidden form I created and submitted the hidden form. After all, remove the created form and IFRAME.
usage:
Asyn. formsubmit (ARGs, action [, func]) Arg: Can Be A form, an input form element, and an array of input form elements. Action: submitted URL. FUNC: callback function
example: Asyn. formsubmit (inputarr, 'xxx. aspx ', function (d) {
alert (d);
});
end
it's over here, here we recommend a self-written JS framework. The above method is integrated into the framework.
usage: copy Code the code is as follows: B $. asyn. formsubmit (inputarr, 'xxx. aspx ', function (d) {
alert (d);
});

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.