How to solve the problem of abnormal data submitted by website by operating img with Javascript
This article will explain in detail how to solve the problem of abnormal data submitted by the website with Javascript operation img. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
Using Javascript to skillfully manipulate img to handle website exception submission data
The functional effects of this paper are as follows:
The form submits data to server A, and if server A cannot access it, submit it to server B.
To use JS to achieve the above functions, you should consider the cross-domain problem. There is a scheme that uses the xmlhttp method of ajax to detect that remote files do not have a return of 404.This scheme is not used in testing because ajax has cross-domain problems, and img's onerror and onload events are also used to deal with it, but this scheme will cause the submitted form IE to pop up, and the browser will intercept the pop-up window, so it does not adopt this scheme. What should I do? After a long time, we finally use js to operate img to deal with it.
Perhaps said so much, you still do not understand what I mean, regardless of these, the following look at the code you will be clear.
Step 1, create a test.htm file with the following contents:
Code
Step 2: create the getMessage.js file as follows:
Code
FunctiongetMessage (frm) {varsendtype=frm.SendType; vartitle=frm.title; / / create a simulated form form varobjfrm=document.createElement ("form"); varobjsendtype=document.createElement ("input"); varobjtitle=document.createElement ("input"); / / use the operation img to process the server page varobjImg=document.createElement ("img") that the target receives data; objImg.id= "TmpSmsImg"; objImg.src= "server" / / default server An image document.body.appendChild (objImg); varimgWidth=document.getElementById ("TmpSmsImg") .width; document.body.removeChild (objImg); if (imgWidth== "210") / / if the picture exists, you can get the page {objfrm.action=" http://www.xueit.com/testGet.aspx"; of server A whose width is equal to the predetermined value and receives data by default. } else// picture does not exist. On the page {objfrm.action= "http://demo.xueit.com/testGet.aspx";} / / of another server B that receives the data, the properties of the form are objfrm.id=" TmpForm "; objfrm.name=" TmpForm "; objfrm.target=" _ 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); / / additional 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 } / / handle the encoding function functionUtf2Gb (str) {if (stringed codes nullable strings = "") str=escape (str); returnstr } this is the end of the article on "how to solve the problem of abnormal data submitted by the website with Javascript operation img". I hope the above content can be helpful to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.