This article focuses on how to use Javascript to skillfully manipulate img to handle abnormal website submission data. You can submit data to server A in two steps, if server A cannot access server B, submit it to server B. For more information, see the following.
Use Javascript to skillfully manipulate img for website exception submission and Data Processing
The functions described in this article are as follows:
The form submits data to server A. If server A cannot access the form, the data is submitted to server B.
To use JS to implement the above functions, we need to consider cross-origin issues. We have used the ajax xmlhttp method to detect that the remote file does not exist and returns 404, this scheme is not used due to cross-origin issues in ajax during testing, and img onerror and onload events are also used for processing, however, this scheme will cause the submitted Form IE to pop up, And the browser intercepts the pop-up window, so this scheme is not used. What should I do? It took a long time to complete the process by using js operation img.
You may not understand what I mean after talking about it. If you don't care about it, you can see the code below.
Step 1: Create a test.htm file with the following content:
Code
-
- <scriptsrcscriptsrc="getMessage.js"></script>
- <body>
- <formnameformname="mfrm">
- <inputidinputid="SendType"type="hidden"
- value="s129"name="SendType"/>
- <inputidinputid="title"name="title"/>
- <inputonClickinputonClick="returngetMessage(this.form);"
- type="button"name="imageField"/>
- </form>
- </body>
-
Step 2: Create the getMessage. js file with the following content:
Code
- FunctiongetMessage (frm ){
- Varsendtype = frm. SendType;
- Vartitle = frm. title;
- // Create a simulated form
- Varobjfrm = document. createElement ("form ");
- Varobjsendtype = document. createElement ("input ");
- Varobjtitle = document. createElement ("input ");
- // Use the img operation to process the server page of the target receiving data
- VarobjImg = document. createElement ("img ");
- ObjImg. id = "TmpSmsImg ";
- ObjImg. src = "http://www.xueit.com/images/logo.gif ";
- // Default server a image
- Document. body. appendChild (objImg );
- VarimgWidth = document. getElementById ("TmpSmsImg"). width;
- Document. body. removeChild (objImg );
-
- If (imgWidth = "210") // if the image exists,
- You can get the page of server A whose width is equal to the predefined value, which receives data by default.
- {
- Objfrm. action = "http://www.xueit.com/testGet.aspx ";
- }
- Else // The image does not exist. The page of server B that receives data
- {
- Objfrm. action = "http://demo.xueit.com/testGet.aspx ";
- }
-
- // The following table attributes
- Objfrm. id = "TmpForm ";
- Objfrm. name = "TmpForm ";
- Objfrm.tar get = "_ blank ";
- Objfrm. method = "post ";
- Objfrm. style. display = "none ";
-
- Objsendtype. type = "hidden ";
- Objsendtype. name = "SendType ";
- Objsendtype. value = Utf2Gb (sendtype );
-
- Objtitle. type = "hidden ";
- Objtitle. name = "title ";
- Objtitle. value = Utf2Gb (title );
-
- // Add
- Objfrm. appendChild (objsendtype );
- Objfrm. appendChild (objtitle );
-
- // Form submission
- Document. body. appendChild (objfrm );
- Objfrm. submit ();
- Document. body. removeChild (objfrm );
-
- // Clearobj
- Objtitle = null;
- Objsendtype = null;
- Objfrm = null;
-
- }
-
- // Process the encoding function
- FunctionUtf2Gb (str)
- {
- If (str! = Null & str! = "")
- Str = escape (str );
- Returnstr;
- }
-
The above code mainly uses img images. If the image server is running normally, the image will be loaded into the body area of the current page, so we can get the width of the img image, if the width is equal to the predefined value, the default server is normal. Otherwise, the Code server A is abnormal and the data is submitted to server B for processing.