跨域表单提交状态的变相判断代码
拐个弯想,如果开始可以获得iframe内部页面信息,那么不能获得的时候,不就说明表单已经提交了么~
我们发现百度的竞价右下角信息,也是采用的这种方法判断的,大家可以参考下。
示例:
<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.php" 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.php
代码如下:
<?php
sleep(5);
echo $_POST['dddd'];
?>
弊端:
这个弊端还很明显,如果是404,403,500等错误,也会误认为是成功提交。
所以,请注意:此方法属研究,开拓思维用。
这段是百度的判断代码
代码如下:
var sfMessTimes;
function sfMessSubmitMonitor () {
try{
var hash = sf_mess_lib.getElement(SF_MESS_FRAME_ID).contentWindow.location.hash;
sfMessTimes ++;
if (sfMessTimes > 50) {
alert(sf_mess_msg.fail);
sf_mess_lib.getElement(SF_MESS_SUBMIT_ID).disabled = false;
for (var i = 0, l = sf_mess_cols.length; i < l; i++) {
document.getElementById(SF_MESS_PREFIX + sf_mess_cols[i].idname).disabled = false;
}
} else {
setTimeout(sfMessSubmitMonitor,100);
}
} catch (e) {
sf_mess_lib.getElement(SF_MESS_FRAME_ID).src = "about:blank";
alert(sf_mess_msg.success);
sf_mess_lib.getElement(SF_MESS_SUBMIT_ID).disabled = false;
for (var i = 0, l = sf_mess_cols.length; i < l; i++) {
var inputCfg = sf_mess_cols[i];
var inputEl = document.getElementById(SF_MESS_PREFIX + inputCfg.idname);
inputEl.disabled = false;
inputEl.value = filtInnertip(inputCfg.innertip);
}
}
}