Javascript prevents repeated submission

Source: Internet
Author: User
In many cases, we need to prevent repeated submissions. There are also many articles in this regard, and there are great differences in implementation methods. the following is a control submission method I wrote. sometimes, even if the server can identify repeated submissions, it may cause problems. for example, if an operation that takes a long wait time is submitted repeatedly after the first submission, the page may die. this problem can be prevented by using scripts. of course, it can also be controlled by both the script and the server, which is perfect.
In other words, place the following script on the top of the page.

1document. isposted = false;
2 function canceldubsubmit ()
3 {
4 If (typeof (event. returnvalue) = "undefined" | event. returnvalue = true )&&! Document. isposted)
5 {
6 Document. isposted = true;
7 event. returnvalue = true;
8}
9 else
10 {
11 event. returnvalue = false;
12}
13}

The following bold parts are placed in the form tag. If you already have other execution functions of the onsubmit event, you can put them together. It is best to put the canceldubsubmit () function at the end. <Form ID = "form1"Onsubmit= "Canceldubsubmit ();"Method = "Post" runat = "server">

Where document. isposted is used to record whether to send back. once the page is sent back, document. isposted will be true. after reload, document. isposted = false will be executed. when the onsubmit event does not have other execution functions or other execution functions, return true and document. if isposted is set to false, the return page is displayed. Otherwise, the return page is stopped.
The above method cannot control the server-side controls using the function _ dopostback () function. because the submission in this function depends on document. form1.submit (), which does not trigger the onsubmit event. we also need to rewrite the _ dopostback () function. function _ dopostback (eventtarget, eventargument ){
If (! Document. isposted)
{
VaR theform;
If (window. Navigator. appname. tolowercase (). indexof ("Netscape")>-1 ){
Theform = Document. Forms ["form1"];
}
Else {
Theform = Document. form1;
}
Theform. _ eventtarget. value = eventtarget. Split ("$"). Join (":");
Theform. _ eventargument. value = eventargument;

Document. isposted = true;
Theform. Submit ();
}
}

Place the above Code after the original _ dopostback () function of the page.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.