I can't trigger the unload event in Chrome

前端 未结 4 920
时光取名叫无心
时光取名叫无心 2020-12-17 18:58

This code runs fine on Firefox, but I can\'t make the unload event work on Chrome anymore. Did Chrome stop supporting the unload event?

This is my c

4条回答
  •  没有蜡笔的小新
    2020-12-17 19:25

    window.onunload = alert("Are you sure 2?");
    

    This is incorrect. You are setting onunload to the result of alert, you need to set it to a function:

    window.onunload = function(){
        alert("Are you sure?");
    }
    

    If you want to use jQuery, this will work in all browsers.

    $(window).unload(function () {
         alert("Are you sure?");
    });
    

    NOTE: It might seem like it's not working in Chrome, but it is. That's because Chrome blocks alerts in the onunload event.

提交回复
热议问题