acctman
acctman

Reputation: 4349

Process a file when user exits page?

this is my code so far, what I'm trying to do is process the io_up.php file when the user leaves the page. It was also be great if i could pause the page load for 3sec after file is processed.

<script type="text/javascript"> 
function setConfirmUnload(on) {
    Window.onbeforeunload = (on) ? unloadMessage : null;
}

function unloadMessage() {

    $.ajax({
       async: true,
       url: "/proc_files/io_up.php",
       success: function(msg){
         alert( "Data Saved.");
       }
     });

}
</script>

Upvotes: 0

Views: 88

Answers (1)

epascarello
epascarello

Reputation: 207501

Change the code to synchronous instead of async. Just remember that you are doing to hang the user's browser.

This code will be called when people leave the page, refresh it, click a link, etc. Is that what you really want?

You really should be handling this with sessions on the server and not relying on the client. Browser errors, people putting their computers to sleep, network errors, etc will never fire your processing code.

Upvotes: 2

Related Questions