TypeError: $(...).unload 不是函数 - JQuery 3.1.0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39030910/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:12:23  来源:igfitidea点击:

TypeError: $(...).unload is not a function - JQuery 3.1.0

jquery

提问by elG

I'm trying to execute a function before redirecting to other page, but when the original page loads I get the error that I wrote in the title:

我试图在重定向到其他页面之前执行一个函数,但是当原始页面加载时,我收到了我在标题中写的错误:

TypeError: $(...).unload is not a function

I've tried this:

我试过这个:

$( window ).unload(function() {
  return "Bye now!";
});

And this:

和这个:

$(window).unload(function(){
  cambiar_ruta();
  alert(ruta);
});

But nothing is working,I don't even get a Leaving page warning.

但是没有任何效果,我什至没有收到离开页面警告。

With next code I don't get an error, but the function is never triggered and the alert never shown:

使用下一个代码,我没有收到错误消息,但从未触发该功能并且从未显示警报:

var ruta;
$(window).bind('onbeforeunload', function(e){
  alert('Bye.');
  cambiar_ruta();
  alert(ruta);
});

I'm using jquery-3.1.0.min

我正在使用 jquery-3.1.0.min

回答by Jekin Kalariya

Try to change like below, because you are using jquery-3

尝试如下更改,因为您使用的是 jquery-3

$(window).on("unload", function(e) {
    alert("call");
    console.log("this will be triggered");
});

回答by Jeaf Gilbert

The correct answer would be this:

正确答案是这样的:

$(window).on("beforeunload", function() { 
    // your codes
});

回答by den232

Unfortunately, as of Chrome on a PC in Apr 2020, this is not triggered

不幸的是,从 2020 年 4 月 PC 上的 Chrome 开始,这不会触发