2

What worked:

My code works across all browsers now and looks like this :

window.onbeforeunload = function(){
   $.ajaxSetup({
   async: false
});


 $.post('./archive_onBeforeUnload.php?', {contact_id: contact_id}, function(){
 }).error(function(xhr, textStatus, error){
    console.log(xhr.statusText);
    console.log(textStatus);
    console.log(error);
    }); 
 }

Setting async to be false within $.post() didn't seem to be enough for it to work for me. I had to put that setting in $.ajaxSetup for it to work across all browsers.


My project is a sort of chat program that I created using HTML and PHP. The issue that I'm having is that if the person closes the window using the X button at the top, there is some PHP that I need to run in order for my program to still run properly. So I did some searching and ended up with this bit of code in:

window.onbeforeunload = function(){
    $.post('./archive_onBeforeUnload.php?', {contact_id: contact_id}, function(){
    }).error(function(xhr, textStatus, error){
        console.log(xhr.statusText);
        console.log(textStatus);
        console.log(error);
    });

}

This calls the archive_beforeUnload.php and runs the little amount of PHP that I need it to. In FireFox, Safari, and Chrome this runs just fine but in IE I seem to have an issue. There doesn't seem to be enough time for IE to actually fully complete the call which messes up the rest of my program a bit. A way that I have around this is to use add a return statement like so:

window.onbeforeunload = function(){
    $.post('./archive_onBeforeUnload.php?', {async: 'true', cache: 'false', contact_id: contact_id}, function(){
    }).error(function(xhr, textStatus, error){
        console.log(xhr.statusText);
        console.log(textStatus);
        console.log(error);
    });
    return 'I need to fix the timer here.  Without this it doesn\'t work in IE';
}

This seems to give IE enough time to actually complete the AJAX call. The thing is, it creates a confirmation window before the window closes every single time, which will get annoying for the user. Is there some way that I can get this working in IE without the need for the return statement? I've looked around the web quite a bit but have yet to find something that works for me.

Hopefully I've been clear enough in my issue.

Edit:

Thought I should maybe add my PHP script:

<?php
session_start();
require_once('client_init.php');

$sqlRequeue = 'UPDATE S2G_CHAT_TRAVELLER_QUEUE SET';
$sqlRequeue .= ' isOpen = 1';
$sqlRequeue .= ' WHERE contactID='.$contact_id;
$resRequeue = $conn_dd->query($sqlRequeue);
$resRequeue->close;

$_SESSION['s2g_chat_messages_shown'] = $default_messages_shown;
?>

1 Answer 1

1

I think the problem is that $.post() is asynchronous, so the function returns before it completes.

It looks like you can use the $.async() function to do the same call synchronously.

EDIT: Or you can change the default AJAX settings to be synchronous.

Sign up to request clarification or add additional context in comments.

3 Comments

Wow... I've set 'async' to be both true and false and it did nothing. I've even used $.ajaxSetup() before to no avail, I'd even fallen on the exact post you linked to. This time I seem to have gotten lucky and it's working. Thank you. I'll edit my code up top to reflect what works for me.
@Francis - I don't think async is an option to $.post, only to $.async.
Oh ... That I didn't know. This whole project is my first time working with AJAX so it's been quite a learning experience.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.