0

i have a php web application, where i use an authentication method. I have a script logout.php in the same directory as the index file. I want that the code in the logout.php be executed if the used mid session decides to exit or navigate away from the page.

ive tried using

function closeIt()
{
    var exit = confirm("Are you sure you want to end this chat session ?");
    if(exit==true){
$.ajax({
   type: "GET",
   url: "logout.php",
   success: function(){ alert("Done");}
 });
    }
}

window.onbeforeunload = closeIt;

i get the confirm box, but i am not getting success, am i doing somethign worng or do i need a new approach all together ?

4
  • possible duplicate of Ajax request with JQuery on page unload Commented Apr 13, 2011 at 17:38
  • have you used firebug or something to see if the AJAX call is failing? Commented Apr 13, 2011 at 17:39
  • no i havent used anything to see if ajax is failing but i have other ajax calls on this page itself they work fine... Commented Apr 13, 2011 at 17:41
  • The duplicate should answer all your questions. Commented Apr 13, 2011 at 17:41

2 Answers 2

1

The Ajax call is performed asynchronously, so the call is made and processing is passed back to the page immediately, which then closes before the ajax call completes.

You need to make a synchronous call to make this work.

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

1 Comment

can you show me how ? im very new to ajax... just working by looking at examples... could you show me in my code ?
0

A in AJAX stands for asynchronous. You may want to use synchronous XMLHttpRequest or return false at the end of closeIt() (that should prevent closing window) and in your success function, change onbeforeunload to null and close the window with window.close()

Comments

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.