开发者

Question about when user closes browser tab or window

开发者 https://www.devze.com 2023-02-23 05:42 出处:网络
I would like to accomplish something like the following: window.onbeforeunload = function() { if (confirm(\"Are you sure that you want to leave the page?\")) {

I would like to accomplish something like the following:

window.onbeforeunload = function() { 
    if (confirm("Are you sure that you want to leave the page?")) {
        //do something
开发者_运维知识库    }
}

However, the above code does not work, where as even if the user clicks no, the refresh request gets submitted.

How do I accomplish what I want?

Thanks.


You don't need to write confirm inside function:

Try as given below:

window.onbeforeunload=function()
{
    return "Are you sure that you want to leave the page?";
}


You can't do anything with the "response" of the user to the onbeforeunload prompt...

Capture user response when using window.onbeforeunload

0

精彩评论

暂无评论...
验证码 换一张
取 消