Javascript alert when user closes the tab ot the window

前端 未结 2 1480
灰色年华
灰色年华 2021-01-22 09:34

I want when a user closes the tab or window or when he tries to move to another location different from my site to pops a confirm box, and if he confirm to execute an ajax scrip

2条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-22 10:19

    $(window).unload(function() {
        var answer=confirm("Are you sure you want to leave?");
    if(answer){
        //ajax call here
        }
    });
    

    Just add your own alert/dialogue code to the function.

提交回复
热议问题