在Asp.net中利用Javascript避免表單重複提交(for .net2.0)

來源:互聯網
上載者:User
 

  1. //---------------------------------------------------------
  2. // 作者:野文(Jasson)
  3. // 日期:2007-1-11
  4. // 功能:避免重複提交資料
  5. //       請放在HTML頁面的最後,</body>標記前</form>標記後。
  6. //---------------------------------------------------------
  7. function _doPostBack(){}; 
  8. if(typeof(__doPostBack)=="function") 
  9. {     
  10.     _doPostBack=__doPostBack; 
  11.     __doPostBack=_doPostBackNew; 
  12. document.attachEvent("onmousemove",_onmousemove); 
  13. var _isPosting=false; 
  14. var _divMask=null; 
  15. var _divMOMask = null;
  16. function _onmousemove() 
  17. {       
  18.     if(_divMOMask) 
  19.         with(_divMOMask.runtimeStyle) 
  20.         {
  21.           left=event.clientX+document.body.scrollLeft-4; 
  22.           top=event.clientY+document.body.scrollTop-4; 
  23.         } 
  24. }
  25. function _makeMOMask() 
  26.     var div=document.createElement("DIV");   
  27.     with(div.runtimeStyle) 
  28.     {
  29.         width="100%";
  30.         height="100%";
  31.         position="absolute";      
  32.       zIndex=999999; 
  33.       fontSize="12px";
  34.       cursor="wait"; 
  35.       backgroundColor="gray"; 
  36.       filter="alpha(opacity=0)";               
  37.     } 
  38.     try 
  39.     {         
  40.       document.body.insertAdjacentElement("BeforeEnd",div);  
  41.       div.style.left=( parseFloat(document.body.offsetWidth)-parseFloat(div.offsetWidth))/2  ;//event.clientX+document.body.scrollLeft-4; 
  42.       div.style.top=(  parseFloat(document.body.offsetHeight)-parseFloat(div.offsetHeight))/2;//event.clientY+document.body.scrollTop-4;                   
  43.       div.onblur=new Function("this.focus()"); 
  44.       div.focus();       
  45.     } 
  46.     catch(x){} 
  47.     if(_divMOMask)    
  48.         _divMOMask.removeNode(true); 
  49.      _divMOMask=div; 
  50. }  
  51. function _makeMask() 
  52.     var div=document.createElement("DIV"); 
  53.     
  54.     with( div )
  55.     {
  56.         innerHTML = "<font color='red'>正在更新頁面,請稍候...</font>";    
  57.         align="center";      
  58.   }
  59.   
  60.     with(div.runtimeStyle) 
  61.     {
  62.         paddingLeft="30px";
  63.         paddingRight="30px";
  64.         paddingTop = "15px";
  65.         paddingBottom="15px";
  66.         position="absolute";      
  67.       zIndex=999998; 
  68.       fontSize="12px";
  69.       cursor="wait"; 
  70.       backgroundColor="#D0D0FF"; 
  71.       filter="alpha(opacity=90)";               
  72.     } 
  73.     try 
  74.     {         
  75.       document.body.insertAdjacentElement("BeforeEnd",div);  
  76.       div.style.left=( parseFloat(document.body.offsetWidth)-parseFloat(div.offsetWidth))/2  ;//event.clientX+document.body.scrollLeft-4; 
  77.       div.style.top=(  parseFloat(document.body.offsetHeight)-parseFloat(div.offsetHeight))/2;//event.clientY+document.body.scrollTop-4;                   
  78.       //div.onblur=new Function("this.focus()");       
  79.       //div.focus();       
  80.     } 
  81.     catch(x){} 
  82.     if(_divMask)    
  83.         _divMask.removeNode(true); 
  84.      _divMask=div; 
  85. function _doPostBackNew(sender,args) 
  86.     if(_isPosting) 
  87.       return event.returnValue=!(event.cancelBubble=true); 
  88.     window.status="正在更新頁面,請稍候...";        
  89.     _doPostBack(sender,args); 
  90.     _isPosting=true; 
  91.     _makeMask();
  92.     _makeMOMask(); 
  93. function _onformsubmit() 
  94.     if(_isPosting) 
  95.       return event.returnValue=!(event.cancelBubble=true); 
  96.     _isPosting=true; 
  97.     _makeMask(); 
  98.     _makeMOMask();   
  99. new function _attachForms() 
  100. {
  101.     if( typeof(WebForm_OnSubmit)=="function" )
  102.     {
  103.         function _WebForm_OnSubmit(){};
  104.         _WebForm_OnSubmit = WebForm_OnSubmit;
  105.         WebForm_OnSubmit = function()
  106.         {
  107.             if( _WebForm_OnSubmit() )
  108.                 _onformsubmit();
  109.         }        
  110.     }
  111.      
  112.     with(new Enumerator(document.forms)) 
  113.     for(;!atEnd();moveNext()) 
  114.     { 
  115.         if( typeof(item().onsubmit)!="function")
  116.           item().attachEvent("onsubmit",_onformsubmit);       
  117.       var div=document.createElement("div"); 
  118.       div.runtimeStyle.width="0px"; 
  119.       div.runtimeStyle.hight="0px"; 
  120.       div.runtimeStyle.overflow="hidden"; 
  121.       div.runtimeStyle.position="absolute"; 
  122.       item(0).insertAdjacentElement("afterbegin",div); 
  123.       div.innerHTML="<INPUT id=webformpatchsubmitelement onclick='return event.returnValue=false' type=submit value=webformpatchsubmitelement name=webformpatchsubmitelement>";     
  124.     } 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.