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