Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to solve the problem of abnormal data submitted by website by operating img with Javascript

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report