check if changes saved before unload

▼魔方 西西 提交于 2019-12-01 23:25:40
Derek 朕會功夫
<body onbeforeunload="return bye()">

function bye(){
    if(changesSaved) {
        return "You havent saved your changes."
    }
}

This is how I do it.

or in pure JavaScript:

window.onbeforeunload = function() {
    if (changesSaved) {
        return "You haven't saved your changes.";
    }
};
window.onbeforeunload = function () {
     var changesSaved = confirm("You havent saved your changes. Are you sure you want to leave the page?");
    if (changesSaved) {
        return true;
    } else {
        return false;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!