Php onbeforeunload事件上的jQuery.ajax不起作用
我有一个像这样的php页面Php onbeforeunload事件上的jQuery.ajax不起作用,php,jquery,Php,Jquery,我有一个像这样的php页面 <script type="text/javascript" src="js/jquery.min.js"></script> <script type="text/javascript"> window.onbeforeunload = saveBeforeExit; function saveBeforeExit() { jQuery.ajax({ url:"truncate_logs.php",
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
window.onbeforeunload = saveBeforeExit;
function saveBeforeExit() {
jQuery.ajax({
url:"truncate_logs.php",
type:"GET",
async:false,
success:function(data){
}
})
}
</script>
//I am creating 'logs_".$t.".xls' here
<?php
header("Location: log_files/logs_".$t.".xls");
?>
window.onbeforeunload=saveBeforeExit;
函数savebeforexit(){
jQuery.ajax({
url:“truncate_logs.php”,
键入:“获取”,
async:false,
成功:功能(数据){
}
})
}
//我正在这里创建“logs”..$t.“.xls”
我的问题就在这里
位置:未调用onbeforeunload。onbeforeunload不工作,因为它的编码不正确。您需要从onbeforeunload返回一个字符串,该字符串将用作将出现的窗口中的消息。您对事件的分配可能来得太早(页面未准备就绪)。
请尝试:
$(document).ready(function() {
window.onbeforeunload = saveBeforeExit;
});
我找到了答案。将async:false添加到jQueryAjax请求中
请参见我遇到了同样的问题,并通过以下方法解决了它:
window.onbeforeunload = function (e) {
var e = e || window.event;
// For IE and Firefox
if (e) {
e.returnValue = '';
}
// For Chrome and Safari
return '';
};
然而Opera似乎并没有在卸载事件之前捕获,太糟糕了