用JS實現在頁面關閉或刷新時觸發特定的事件


 用JS實現在頁面關閉或刷新時觸發特定的事件
  <script type="text/javascript">
window.onbeforeunload = function()  
{  
  var n = window.event.screenX - window.screenLeft;  
  var b = n > document.documentElement.scrollWidth-20;  
  if(b && window.event.clientY < 0 || window.event.altKey)  
  {  
          alert("是關閉而非刷新");  
          document.getElementById("ButtonClear").click(); 
        //window.event.returnValue = "";  
  }
else  
  {  
      alert("是刷新而非關閉");  
  }  
}
  
 </script>
 
注:在用document.getElementById("ButtonClear").click(); 時出現為對象為空,意思就是未找到這個按鈕,發現原因是在按鈕的屬性里設置了Visable="false",應該將其改為:style="display:none"
 
因此在這里要比較一下按鈕的 Visable="false" 屬性和 style="display:none" 屬性設置的區別:
style="display:none" 僅僅是隱藏了控件,但是html代碼依然生成,而   Visable="false"     就直接不生成html控件,也就是頁面上沒有這個控件對象了。
 
所以,如果要用document.getElementById("ButtonClear").click();  的話,就只能將按鈕的屬性設置成為 style="display:none"


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM