Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery.ajax on onbeforeunload event not working

Tags:

jquery

php

I have one php page like this

<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");
?>

My problem is here on

Location:onbeforeunload is not getting called.

like image 337
Wazy Avatar asked Dec 08 '25 01:12

Wazy


2 Answers

I found the answer. Add async : false to your jquery ajax request.

See How can I get jQuery to perform a synchronous, rather than asynchronous, Ajax request?

like image 183
Anthony Avatar answered Dec 10 '25 16:12

Anthony


onbeforeunload is not working because it isn't coded properly. You need to return a string from onbeforeunload , which will be used as the message in the window that will appear.

like image 21
kgiannakakis Avatar answered Dec 10 '25 14:12

kgiannakakis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!