现在的位置: 首页 > 综合 > 正文

关闭窗体事件-onbeforeunload事件

2012年12月18日 ⁄ 综合 ⁄ 共 1676字 ⁄ 字号 评论关闭

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

用法:

object.onbeforeunload = handler
<element onbeforeunload = “handler” … ></element>
描述:
事件触发的时候弹出一个有确定和取消的对话框,确定则离开页面,取消则继续待在本页。
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

平台支持:
IE4+/Win, Mozilla 1.7a+, Netscape 7.2+, Firefox0.9+

<script language="javascript">
var g_blnCheckUnload = true;
function RunOnBeforeUnload() {
if (g_blnCheckUnload) {window.event.returnValue = 'You will lose any unsaved content';
}
}
function turnToOtherPage(){
g_blnCheckUnload = false;
//在此处添加你自己需要处理的方法,或页面跳转的连接
g_blnCheckUnload = true;
}
</script>
<body onbeforeunload="RunOnBeforeUnload()">
<input type="button" name="TrampEagle" value="TurnToPage" onclick="turnToOtherPage()">
</body>

<script language="javascript">
  g_blnCheckUnload = true;
  function RunOnBeforeUnload() {
     if (g_blnCheckUnload) {window.event.returnValue = 'You will lose any unsaved content';  
     } 
  }
</script>
<body  onbeforeunload="RunOnBeforeUnload()">
</body>
这样每次关闭浏览器窗口或涮新页面时,都要触发onbeforeunload这个事件,这样可以对用户进行提示,避免不必要的遗憾。

<script  language=javascript> 
function  window.onbeforeunload() 

   if(event.clientX>document.body.clientWidth&&event.clientY<0  ¦  ¦event.altKey) 
   { 
       var  xmlhttp  =  new  ActiveXObject("Microsoft.XMLHTTP"); 
       xmlhttp.open("GET","sessionEnd.asp",false); 
       xmlhttp.send(); 
     } 

</script>

 

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/boy_north/archive/2006/06/06/775869.aspx

【上篇】
【下篇】

抱歉!评论已关闭.