Javascript cleverly solves the problem of abnormal website submission by img (1)

Source: Internet
Author: User

This article describes how to use javascript to skillfully operate img for abnormal website submission. If A table needs to submit data to server A, it will be submitted to server B if server A cannot access it, it is a good choice to use js operation img to complete the process.

Use javascript to skillfully manipulate img for website exception submission

To submit data in A form to server A, if server A cannot access server B, submit the data to server B. To use JS to implement the aforementioned functions, you must consider cross-origin issues, I have used the xmlhttp method of ajax to detect that the remote file does not return 404. This method is not used due to cross-origin issues in ajax during testing, I have also used img's onerror and onload event solutions to handle this problem, but this solution will cause the submitted Form IE to pop up, And the browser intercepts the pop-up window, so I do not use this solution, what should I do? After a long time, the Javascript operation img is used to complete the process. The specific steps are as follows.

Step 1: Create a test.htm File

The content is as follows:

 
 
  1. <scriptsrcscriptsrc="getMessage.js"></script> 
  2. <body> 
  3. <formnameformname="mfrm"> 
  4. <inputidinputid="SendType" 
  5. type="hidden"value="s129"name="SendType"/> 
  6. <inputidinputid="title"name="title"/> 
  7. <inputonClickinputonClick="returngetMessage(this.form);
  8. "type="button"name="imageField"/> 
  9. </form> 
  10. </body> 

Step 2: Create the getMessage. js File

The content is as follows:

 
 
  1. FunctiongetMessage (frm ){
  2. Varsendtype = frm. SendType;
  3. Vartitle = frm. title;
  4. // Create a simulated form
  5. Varobjfrm = document. createElement ("form ");
  6. Varobjsendtype = document. createElement ("input ");
  7. Varobjtitle = document. createElement ("input ");
  8. // Use the img operation to process the server page of the target receiving data
  9. VarobjImg = document. createElement ("img ");
  10. ObjImg. id = "TmpSmsImg ";
  11. ObjImg. src = "http://www.xueit.com/images/logo.gif ";
  12. // Default server a image
  13. Document. body. appendChild (objImg );
  14. VarimgWidth = document. getElementById ("TmpSmsImg"). width;
  15. Document. body. removeChild (objImg );
  16.  
  17. If (imgWidth = "210 ")
  18. // If the image exists, you can get the width equal to the predefined value,
  19. Page of server A receiving data by default
  20. {
  21. Objfrm. action = "http://www.xueit.com/testGet.aspx ";
  22. }
  23. Else // The image does not exist. The page of server B that receives data
  24. {
  25. Objfrm. action = "http://demo.xueit.com/testGet.aspx ";
  26. }
  27.  


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.