预防"提交"按钮的多次点击-onbeforeunload事件


预防"提交"按钮的多次点击



Google,找到了一些代码,自己修改了一下。主要参考老猫'blog.


public void page_load(Object obj,EventArgs e) 

  btn.Attributes.Add("onclick","state=true;"); 
  StringBuilder sb=new StringBuilder(); 
  sb.Append("if (!state) return;"); 
  sb.Append("var button=document.getElementById('btn');"); 
  sb.Append("button.value=/" Please Wait... /";"); 
  sb.Append("document.body.style.cursor='wait';"); 
  sb.Append("button.disabled=true;"); 

 string strScript="


这样每次关闭浏览器窗口或涮新页面时,都要触发onbeforeunload这个事件,这样可以对用户进行提示,避免不必要的遗憾。

我上网又查了一些这个事件的用法,感觉还是http://www.surfchen.org/?p=54这篇写得最清楚了,摘抄如下:

onbeforeunload事件: 
注:三大主流浏览器中firefox和IE都支持onbeforeunload事件,opera尚未支持。

用法:

object.onbeforeunload = handler  描述:
事件触发的时候弹出一个有确定和取消的对话框,确定则离开页面,取消则继续待在本页。
handler可以设一个返回值作为该对话框的显示文本。

 

触发于:

  • 关闭浏览器窗口
  • 通过地址栏或收藏夹前往其他页面的时候
  • 点击返回,前进,刷新,主页其中一个的时候
  • 点击 一个前往其他页面的url连接的时候
  • 调用以下任意一个事件的时候:click,document write,document open,document close,window close ,window navigate ,window NavigateAndFind,location replace,location reload,form submit.
  • 当用window open打开一个页面,并把本页的window的名字传给要打开的页面的时候。
  • 重新赋予location.href的值的时候。
  • 通过input type=”submit”按钮提交一个具有指定action的表单的时候。

可以用在以下元素:
BODY, FRAMESET, window

你可能感兴趣的:(DOTNET,浏览器,function,javascript,firefox,opera,action)