javascript 防止重复提交

javascript 防止重复提交

很多时候我们都需要防止重复提交,这方面的文章也比较多,实现的途径差别也很大.以下是我写的一种控制提交的方式.因为有些时候即使服务器能够识别重复的提交,也会造成问题.比如需要很长等待时间的操作,在首次提交后,不断重复提交,页面可能会死掉.用脚本来控制的话可以防止这种问题.当然也可以脚本和服务器都进行控制,这样就比较完美了.

    闲话少说,将以下脚本放置于页面顶部.

 1
None.gif
document.IsPosted 
=
 
false
;

 2
None.gif
function
 CancelDubSubmit()

 3
ExpandedBlockStart.gifContractedBlock.gif
dot.gif

 4InBlock.gif    if ((typeof(event.returnValue) == undefined || event.returnValue== true&& !document.IsPosted)
 5ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif
 6InBlock.gif        document.IsPosted = true;
 7InBlock.gif        event.returnValue = true;
 8ExpandedSubBlockEnd.gif    }

 9InBlock.gif    else
10ExpandedSubBlockStart.gifContractedSubBlock.gif    dot.gif{          
11InBlock.gif        event.returnValue = false;
12ExpandedSubBlockEnd.gif    }

13ExpandedBlockEnd.gif}

以下加粗部分放置于form标签中,如果你已经有了onsubmit事件的其他执行函数,可以放在一起,最好将CancelDubSubmit()函数放在最后.

None.gif
<
form 
id
=”Form1″
 onsubmit
=”CancelDubSubmit();”
 method
=”post”
 runat
=”server”
>

    其中document.IsPosted是为了记录是否回送.一旦页面回送,document.IsPosted将为true. 重新加载后,document.IsPosted=false将被执行.当onsubmit事件没有其他执行函数或者其他执行函数返回true并且document.IsPosted=false时,回送页面,否则停止回送.

    以上方法不能控制使用 function __doPostBack() 函数的服务器端控件.因为在此函数中的提交是靠 document.Form1.submit() 实现的,它不会触发Onsubmit事件.那么我们还需要重写__doPostBack() 函数.

ExpandedBlockStart.gif
ContractedBlock.gif
function
 __doPostBack(eventTarget, eventArgument) 
dot.gif
{

InBlock.gif    
if (!document.IsPosted)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{

InBlock.gif        
var theform;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
if (window.navigator.appName.toLowerCase().indexOf(netscape> 1dot.gif{

InBlock.gif            theform 
= document.forms[Form1];
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
else dot.gif{

InBlock.gif            theform 
= document.Form1;
ExpandedSubBlockEnd.gif        }

InBlock.gif        theform.__EVENTTARGET.value 
= eventTarget.split($).join(:);
InBlock.gif        theform.__EVENTARGUMENT.value 
= eventArgument;    
InBlock.gif        
InBlock.gif        document.IsPosted 
= true;
InBlock.gif        theform.submit();
ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

请将以上代码放置于页面的原__doPostBack() 函数之后.

转载于:https://www.cnblogs.com/redfire0922/archive/2006/06/20/430746.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/111143.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)
blank

相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号