window.onbeforeunload的替代方法,用于发送数据? [英] Alternatives to window.onbeforeunload for sending data?

查看:572
本文介绍了window.onbeforeunload的替代方法,用于发送数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的AJAX函数访问我的PHP脚本.目的是在用户关闭页面时删除空白的MySQL条目.

The AJAX function below accesses my PHP script. The intention was to delete blank MySQL entries when the user closes the page.

window.onbeforeunload可以完美地实现这一目标,但是在最新版本的Chrome中,它似乎只允许运行警报.

window.onbeforeunload would have been perfect to achieve this, but in the latest version of Chrome it only seems to allow alerts to be run.

想知道这种方法是否有可比的替代方案?

Wondering if there are any comparable alternatives to this approach?

代码示例(不适用于Chrome):

Code sample (doesn't work in Chrome):

window.onbeforeunload = function() {
saveFormData();
return null;
}

function saveFormData() {
$.post("<?php echo site_url('resume/cleanup'); ?>", { resume_id: "<?php echo $this->session->userdata('resume_id'); ?>" } );
}

推荐答案

这是不可靠的.如果用户断开连接,则不会发送任何数据.最好运行Cron作业来清理剩余的数据.

This is not dependable. If the user loses their connection no data will be sent. You're better off running a Cron job to clean up left-over data.

这篇关于window.onbeforeunload的替代方法,用于发送数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆