Jquery: window.unload not working with chrome 72.0.3626.109

浪尽此生 提交于 2019-12-24 02:56:31

问题


I am creating a web app using asp.net mvc. I want to detect a manual refresh by a user,

but I am not able to get the following events

1: beforeunload

2: unload

the following way I Tried

1

$(window).on('beforeunload', function () {
    pageCleanup();
});

2

$(window).on("unload", function () {
    pageCleanup();
});

function pageCleanup() {  
    alert(1)
}

3

window.addEventListener("unload", logData);

function logData() {
    alert(1)
}

4

$(window).unload(function () {
    alert('1');
});

the above is an example with alert, I want to have an ajax call in unload function

but none of these seems to execute when a user press any kind of refresh(F5, ctrl-shift-R, ctrl-R, from url tab)

what should I try now?


回答1:


Hi Check Once and Let me know

<html>
<head>
<title>Refresh a page in jQuery</title>

<script type="text/javascript" src="jquery-1.4.2.min.js"></script>

</head>

<body>

<h1>Stop a page from exit with jQuery</h1>

<button id="reload">Refresh a Page in jQuery</button>

<script type="text/javascript">

    $('#reload').click(function() {

        location.reload();

    });

    $(window).bind('beforeunload', function(){
        return '>>>>>Before You Go<<<<<<<< \n bro!!Working';
    });

</script>

</body>
</html>



回答2:


You should use bind for beforeunload:

$(window).bind("beforeunload", () => alert(1));
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>


来源:https://stackoverflow.com/questions/54800861/jquery-window-unload-not-working-with-chrome-72-0-3626-109

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!