Jquery 事件在离开页面时触发
我试图在用户离开页面时触发一个事件,这意味着更改选项卡等。到目前为止,我已经做到了这一点:Jquery 事件在离开页面时触发,jquery,Jquery,我试图在用户离开页面时触发一个事件,这意味着更改选项卡等。到目前为止,我已经做到了这一点: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
<script>
$(window).on('beforeunload', function() {
alert('Youre trying to leave this page');
});
$(document).ready(function() {
alert('test');
});
</script>
<title>Bounce catcher</title>
</head>
<body>
hdjkfhdskjfhdskhfds
</body>
</html>
$(窗口).on('beforeunload',function()){
警报(“您正试图离开此页面”);
});
$(文档).ready(函数(){
警报(“测试”);
});
弹跳捕手
hdjkfhdskjfhdskhfds
但它不起作用。“测试”警报正常显示,但beforeunload事件上的另一个警报不起作用,显示为“无”。无论是更改选项卡还是关闭选项卡
我在osx上使用chrome,怎么了?控制台也不会显示任何内容。将事件更改为简单的“卸载”也不起作用。将窗口功能放在文档块内不起作用。
<script>
$(window).on('beforeunload', function() {
return 'Youre trying to leave this page';
});
$(document).ready(function() {
alert('test');
});
</script>
$(窗口).on('beforeunload',function()){
返回“您正试图离开此页面”;
});
$(文档).ready(函数(){
警报(“测试”);
});
在函数中用return替换警报,请记住卸载之前
必须在函数中使用return,此函数才能工作
$(window).on('beforeunload', function() {
return 'Youre trying to leave this page';
});
您不能,只需在卸载事件之前检查文档的
。您应该返回一个字符串,但这是浏览器实现的依赖项,因此如何才能执行此操作?return'Youre trusting to leave this page'代码>而不是alert()
AFAIK,FF(版本?)不允许自定义字符串是的,是的,我知道它有时不起作用。还有别的办法吗?
$(window).bind('beforeunload', function(){
return 'Youre trying to leave this page';
});