Jquery Onbeforeunload

Jquery Onbeforeonload

jquery 中的这个简单代码没有获取卸载事件。控制台显示正在禁用缓存。

我希望用户在点击我页面上每个离开页面的 link 时得到提示,但在他们关闭页面时不提示。任何建议。

<html>

<script src="jquery-3.1.1.min.js"></script>

<body>

<a href="https://www.science.gov" id="navigate">science</a>

</body>

<script>

    $(window).on('beforeunload', function(){
    return('Are you sure you want to leave?')};

</script>

</html>

你的代码有错误,你在最后错了 )

Uncaught SyntaxError: missing ) after argument list

将您的代码更改为: $(window).on('beforeunload', function(){ return('Are you sure you want to leave?')});

return不是函数,不会提示。 尝试 return prompt();

<html> 
<script src="jquery-3.1.1.min.js"></script>
 <body> 
<a href="https://www.science.gov" id="navigate">science</a>
 </body> 
<script> 
$(window).on('beforeunload', function(){ return prompt('Are you sure you want to leave?');});
 </script> </html>