$(window).bind(
'beforeunload'
,
function
(){
return
'您输入的内容还没有保存,肯定离开此页面吗?'
;
});
$(window).unbind(
'beforeunload'
);
js离开页面提示 onbeforeunload事件方法
window.onbeforeunload =
function
(event) {
jquery
return
confirm(
"肯定退出吗"
);
浏览器
}
url
如下操做触发beforeunload,onbeforeunload
1 ·关闭浏览器窗口
2·经过地址栏或收藏夹前往其余页面的时候
3·点击返回,前进,刷新,主页其中一个的时候
4·点击 一个前往其余页面的url链接的时候
5·调用如下任意一个事件的时候:click,document.write()方法(输出内容),document.open() 打开一个新的空白文档,document.close()方法可关闭一个由open()方法打开的输出流,并显示选定的数据。
,window close (),form.submit.
6·当用window open打开一个页面,并把本页的window的名字传给要打开的页面的时候。
7·从新赋予location.href的值的时候。
8·经过input type=”submit”按钮提交一个具备指定action的表单的时候。
9.能够用在如下元素:
body, frameset, window
// 关闭窗口时弹出确认提示
$(window).bind('beforeunload', function(){
// 只有在标识变量is_confirm不为false时,才弹出确认提示
if(window.is_confirm !== false){
return '您可能有数据没有保存';
}
});
// 提交表单时,不弹出确认提示框
$('form').bind('submit', function(){
is_confirm = true;
});
//页面内的跳转操做均不弹出确认窗口
$(window).bind('mouseover mouseleave', function(event){
is_confirm = event.type == 'mouseleave';
});
(function(){
// 关闭窗口时弹出确认提示
$(window).bind('beforeunload', function(){
// 只有在标识变量is_confirm不为false时,才弹出确认提示
if(window.is_confirm !== false)
return '您可能有数据没有保存';
})
// mouseleave mouseover事件也能够注册在body、外层容器等元素上
.bind('mouseover mouseleave', function(event){
is_confirm = event.type == 'mouseleave';
});
})();
<script type="text/javascript"> var changeFlag=false;//标识文本框值是否改变,为true,标识已变 $(document).ready(function(){ //文本框值改变即触发 $("input[type='text']").change(function(){ changeFlag=true; }); //文本域改变即触发 $("textarea").change(function(){ changeFlag=true; }); }); //离开页面时保存文档 window.onbeforeunload = function() { if(changeFlag ==true){ //若是changeFlag的值为true则提示 if(confirm("页面值已经修改,是否要保存?")){ //处理信息保存... alert("即将执行保存操做..."); }else{ //不保存数据... alert("不保存信息..."); } }