onbeforeunload wait for ajax result

前端 未结 4 1438
梦谈多话
梦谈多话 2021-01-23 15:32

Is it possible for onbeforeunload() function to wait for ajax result and move to a jsp based on the resultvalue or atleast show a message before unloading?

4条回答
  •  -上瘾入骨i
    2021-01-23 16:25

    AFAIK, it is not possible. onbeforeunload has very limited functionality, otherwise malicious sites could stop you from leaving their site.

    Addendum: It is possible to show a message like 'You are leaving this page'. In that case, the beforeunload() function must return a string.

    Example

    window.onbeforeunload = function() {
       return 'Are you sure about that?';
    }
    

提交回复
热议问题