6

Is there any possibility how to send AJAX after close the browser window?

I have a browser game with movement in JavaScript (jQuery) and if I send Ajax after each movement it would be difficult for the server. So I want to send one AJAX when user close the window (or bookmark). It must be functional in all modern browsers.

Thank you for answers

Darkry
  • 590
  • 2
  • 12
  • 26

5 Answers5

5

I'd suggest you update the server on some sort of timer so the server never gets too far behind in knowing what state the client is in (perhaps every 60 seconds when the client is active), pausing server updates when the client is not active.

Then, in your user interface, put some obvious user interface elements for Close or Stop that encourages the user to shut-down that way and then update the server when either of those buttons are hit.

Then, you can also hook the unload event for the page and send one last ajax call then. But, this is not called in every case or supported in all browsers so this would be done in addition to the two previous techniques.

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • OK. Thank you. I will save the game each 60 seconds (for example) and then I add a button to save the game. And on some important events (like battle) I will send the AJAX at the moment. – Darkry Jul 29 '11 at 06:43
1

I don't think there is a practical way to do it... but there is definitely a solution to your problem.

You can send your request either at some time interval or when the game arrives at a particular stage.

We're not seeing the complete scenario so please evaluate a bit more so I or someone else can help.

If possible.. I would add a "Save State" or just "Save" button. So the user knows that if he doesn't hit "Save" nothing will be "Saved".

Oliver M Grech
  • 3,071
  • 1
  • 21
  • 36
1

You can try window.onbeforeunload e.g.:

function saveGame(e) {
     if (!e) e = window.event;

    //Ajax here
}
window.onbeforeunload = saveGame;
Marc Uberstein
  • 12,501
  • 3
  • 44
  • 72
  • And this works in all modern browsers? Because window.onunload not. – Darkry Jul 29 '11 at 06:58
  • As far as know, yes. I used this for a widget ordering system to alert before closing the tab/window. Had no crossbrowser issues. See this link : http://stackoverflow.com/questions/158673/onbeforeunload-support-detection – Marc Uberstein Jul 29 '11 at 07:11
0

I can suggest this:

  1. invoke window.onunload (pay attention to firefox!) and store the current location in the server. important: make this async call.
  2. in the server save the state of the user (leaving a page)
  3. write a code in the global request event handler and query this state.
  4. you can launch threads in the server to invoke the final unload (say, after 5 sec) no new request from the client

I know these steps are hard to implement, but they address your problem and solves it.

alex
  • 479,566
  • 201
  • 878
  • 984
Karan Shah
  • 744
  • 5
  • 15
0

You can't send any ajax request after closing browser window. But you can use onUnload event for send ajax request when user click close button of the window.

nidhin
  • 6,661
  • 6
  • 32
  • 50