When submitting a form to a cross-domain URL within an iframe, how to determine whether the submission is successful?
This, basically, is difficult, because due to the limitations of the browser security sandbox, we have no way to obtain information about pages in different domains within the iframe. Thinking differently, if you can initially obtain the iframe internal page information, then when you can’t obtain it, doesn’t it mean that the form has been submitted~
The business center guestbook provided by Baidu for bidding users uses this method.
Example:
test.html:
program code
<html>
<head><meta http-equiv="Content-Type" content="text/html; charset=gb2312"></head>
<body style="background:#a7a7a7;">
<iframe id="testiframe" name="testiframe"></iframe>
<form method="post" action=" http://www.test.com/testaction.asp " target="testiframe" name="testform">
<input type="text" name="dddd"><input type="submit" value="ddd" name="submitbtn"/>
</form>
</body>
<script>
document.testform.onsubmit = function () {
document.testform.submitbtn.disabled = true;
submitMonitor();
}
function submitMonitor () {
try{
var hash = document.getElementById('testiframe').contentWindow.location.hash;
setTimeout(submitMonitor,100);
} catch (e) {
document.getElementById('testiframe').src = "about:blank";
document.testform.submitbtn.disabled = false;
}
}
</script>
</html>
testaction.asp
program code
<%
Function Sleep(n) 'Unit seconds s
Dim StartTime
StartTime = Timer
Do: Loop Until Timer>n+StartTime
End Function
Sleep(5) 'Delay 5 seconds
Response.Write Request.Form("dddd")
%>
Disadvantages:
This disadvantage is still obvious. If it is an error such as 404, 403, 500, etc., it will be mistaken for a successful submission. However, it can be
program code
setTimeout(submitMonitor,100);
Add a client form check before the function to try to avoid this problem.
Of course, cross-domain form submission status judgment can also be implemented by outputting an alert script on the server side.