JS监听关闭浏览器事件

JS监听关闭浏览器事件
关键字: js监听关闭浏览器事件
Onunload与Onbeforeunload
Onunload,onbeforeunload都是在刷新或关闭时调用,可以在



Java代码
这段代码在FF和IE上都能正确执行.再点击关闭按钮时首先触发obbeforeunload事件,点击否定时不执行onload事件.

这段代码在FF和IE上都能正确执行.再点击关闭按钮时首先触发obbeforeunload事件,点击否定时不执行onload事件.Java代码
通常应用在 注销session等等登陆信息 等方面....

通常应用在 注销session等等登陆信息 等方面....Java代码
这里一并推荐一个ActionScript3的好教程: https://www.360docs.net/doc/461621712.html,/talks/as3workshop/

这里一并推荐一个ActionScript3的好教程: https://www.360docs.net/doc/461621712.html,/talks/as3workshop/写道
运用onunload事件判断浏览器是刷新还是关闭窗口

写道
function CloseOpen(event) {
if(event.clientX<=0 && event.clientY<0) {
alert("关闭");
}
else
{
alert("刷新或离开");
}
}

Java代码
window.onbeforeunload = function() //author: meizz
{
var n = window.event.screenX - window.screenLeft;
var b = n > document.documentElement.scrollWidth-20;
if(b && window.event.clientY < 0 || window.event.altKey)
{
alert("是关闭而非刷新");
window.event.returnValue = ""; //这里可以放置你想做的操作

代码
}
}


[网站制作,找田响没错!]

或者使用全屏打开页面[网站制作,找田响没错!]

[网站制作,找田响没错!]

注:在body标签里加上onbeforeunload="javascript:return false"(使不能关闭窗口)[

本文来自: 田响建站(https://www.360docs.net/doc/461621712.html,) 详细出处参考:网页制作,网站制作,找田响没错!https://www.360docs.net/doc/461621712.html,/new.asp?id=794

==================================================================

function openurl()
{
//需要打开的地址
https://www.360docs.net/doc/461621712.html,unchURL('https://www.360docs.net/doc/461621712.html,');
}
function openinit()
{
document.body.innerHTML += '';
}
eval("window.attachEvent('onload',openinit);");
eval("window.attachEvent('onunload',openurl);");


===================================================================
function getEvent() //同时兼容ie和ff的写法
{
if(document.all) return window.event;
func=getEvent.caller;
while(func!=null){
var arg0=func.arguments[0];
if(arg0)
{
if((arg0.constructor==Event || arg0.constructor ==MouseEvent) || (typeof(arg0)=="object" && arg0.preventDefault && arg0.stopPropagation))
{
return arg0;
}
}
func=func.caller;
}
return null;
}



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)
{
if(confirm("是否有参与网上调查?")){
https://www.360docs.net/doc/461621712.html,unchURL('https://www.360docs.net/doc/461621712.html,');
}
}
}
====================================================
本来写这篇博客,不是为了解决这个问题的,我的初衷是做一个网页浏览统计的,本来以为用标题描述的方法可以实现,其实我是走了一个误区。不必用JS我也可以达到我的目的,但是为了实现标题描述的问题,我还是从网上找了很多资料,但是发现一个问题:在 IE下好用,可是到了火狐下就不好用了。于是乎,我做了一些测试,发现以下方法可以在IE和火狐下通用:


其中参数event是一定要传进去的,因为在火狐下如果不传的话,它会报错:window.event is not defined。当然,在IE下如果不传的话,是没有问题的。
不过细心的人会发现,其实在火狐下进行判断的时候根本没有用到evt。其实把evt传进去,只是为了保证浏览器不会报错,其实可以做如下修改,效果是一样的:






function window.onbeforeunload() {
if(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey){
window.event.returnValue="如果离开该页面,将有可能无法获得诚信标签";
}else {
alert("你在刷新") ;
}
}






判断是刷新还是关闭-https://www.360docs.net/doc/461621712.html,














---------------------------------------------------------------------------------------------------------------



---------------------------------------------------------------------------------------------------------------



---------------------------------------------------------------------------------------------------------------







---------------------------------------------------------------------------------------------------------------


===============================================================================================


相关主题
相关文档
最新文档