浏览器窗口关闭事件的监听搜集
方式一:(适用与IE浏览器,而且刷新不提示,只在点击浏览器关闭按钮的时候提示)
<script type="text/javascript">
window.onbeforeunload=onclose;
function onclose()
{
if(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey)
{
return "您要离开吗?";
}
}
</script>
----------------------------------------------------------------------------------------------------
方式二:适用与IE和FF,不区分刷新和关闭
<script type="text/javascript">
window.onbeforeunload = onbeforeunload_handler;
window.onunload = onunload_handler;
function onbeforeunload_handler(){
var warning="确认退出?";
return warning;
}
function onunload_handler(){
var warning="谢谢光临";
alert(warning);
}
</script>
---------------------------------------------------------------------------------------------------
方式三:适用与IE和FF,不区分刷新和关闭,最简单的
<script type="text/javascript">
window.onbeforeunload=onclose;
function onclose()
{
return "您确定退出吗?";
}
</script>
---------------------------------------------------------------------------------------------------
方式四:适用与IE和FF,不区分刷新和关闭,稍复杂的
<script language="javascript">
var MSG_UNLOAD="如果你此时离开档案系统,所做操作信息将全部丢失,是否离开?";
var UnloadConfirm = {};
//启用监听浏览器刷新、关闭的方法
UnloadConfirm.set = function(confirm_msg){
window.onbeforeunload = function(event){
event = event || window.event;
event.returnValue = confirm_msg;
}
}
//关闭监听浏览器刷新、关闭的方法
UnloadConfirm.clear = function(){
window.onbeforeunload = function(){};
}
UnloadConfirm.set(MSG_UNLOAD);
</script>
--------------------------------------------------------------------------------------------------
方式五:只适用于IE6下的关闭按钮和快捷键关闭的,刷新不提示
<script type="text/javascript">
window.onbeforeunload=onclose;
function onclose()
{
var warnning = '<fmt:message key="systemMessage.exitWarning" />';
var beforeExit='<fmt:message key="systemMessage.beforeExitWarning" />';
if(event.clientY<0 && event.clientX>document.body.clientWidth-20 || event.clientY<0 && event.clientX<20 ||
event.altKey || event.ctrlKey || event.clientY>document.body.clientHeight){
alert(beforeExit);
return warnning;
}
}
</script>
****************************************************************************************************
另附判断浏览器类型的JS
<script type="text/javascript">
var Sys = {};
var ua = navigator.userAgent.toLowerCase();
if (window.ActiveXObject)
Sys.ie = ua.match(/msie ([\d.]+)/)[1]
else if (document.getBoxObjectFor)
Sys.firefox = ua.match(/firefox\/([\d.]+)/)[1]
else if (window.MessageEvent && !document.getBoxObjectFor)
Sys.chrome = ua.match(/chrome\/([\d.]+)/)[1]
else if (window.opera)
Sys.opera = ua.match(/opera.([\d.]+)/)[1]
else if (window.openDatabase)
Sys.safari = ua.match(/version\/([\d.]+)/)[1];
//以下进行测试
if(Sys.ie) document.write('IE: '+Sys.ie);
if(Sys.firefox) document.write('Firefox: '+Sys.firefox);
if(Sys.chrome) document.write('Chrome: '+Sys.chrome);
if(Sys.opera) document.write('Opera: '+Sys.opera);
if(Sys.safari) document.write('Safari: '+Sys.safari);
</script>
---------------------------------------------------------------------------------------------------
区分浏览器,IE和FF分别处理(奇怪的是,IE下有时候失效)
<script type="text/javascript">
window.onbeforeunload=onclose;
function onclose()
{
var Sys = {};
var warnning = '<fmt:message key="systemMessage.exitWarning" />';
var ua = navigator.userAgent.toLowerCase();
if (window.ActiveXObject)
Sys.ie = ua.match(/msie ([\d.]+)/)[1]
else if (document.getBoxObjectFor)
Sys.firefox = ua.match(/firefox\/([\d.]+)/)[1]
if(Sys.ie) {//for IE
if(event.clientX>document.body.clientWidth&&event.clientY<0||event.altKey)
{
window.event.returnValue = warnning ;
}
}
if(Sys.firefox) //for FF
return warnning;
}
</script>
--------------------------------------------
最简单的判断浏览器类型的方法
<script type="text/javascript">
if(-[1,]){
alert("这不是IE浏览器!");
}else{
alert("这是IE浏览器!");
}
</script>
[1,]在标准浏览器会返回字符串"1",相当于调用[1,].toString,
,IE则返回"1,"。但是这样IE与标准都会通过检测,因此使用负号强制转换为数字,
标准能成功转换为1,1会在if中自动转换为true,而IE则转换为NaN,再自动转换为false!
补充:web前端 , JavaScript ,