页面跳转或关闭之前做某些处理

1 在body上加入onbeforeunload事件
<body onbeforeunload="sendEmail()">
用一个定时器监听,如果点击了发送,则会执行某些自定义处理。

function sendEmail() 
{			
    var emailNum = $("#emailNum").val();
    if (!refresh_token && saveBeforeUnload == false &&            ($("#memberLevel").val() == 4) &&					$("#emailNotice").length > 0 && $("#emailNum").val() > 0) {
	if (confirm("是否發送「新增項目通知電郵」給參與此項目的同事?")) {
		saveBeforeUnload = true;
	}
    }
    return false;
}

var saveBeforeUnload = false;
			
setInterval(function() {
    if(saveBeforeUnload) {
	saveBeforeUnload = false;
        $("#emailNotice").trigger("click");
    }
}, 500);

你可能感兴趣的:(页面跳转)