how can i prevent onbeforeunload from firing when a certain condition is met?

前端 未结 5 1376
眼角桃花
眼角桃花 2020-12-11 16:46

i have a page on which i want to confirm if the user wants to leave. i have to confirm only when a certain condition is met so i wrote code like this

var bac         


        
5条回答
  •  情歌与酒
    2020-12-11 17:21

    Return a string if you want to offer an option to the user to abort the unload. Return nothing in other cases.

    var back = false;
    back = true; //Somewhere, the condition is set to true
    window.onbeforeunload = function (e) {
        if(back == true)
            return "Are you sure to exit?";
    }
    

提交回复
热议问题