Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any possibility how to send an AJAX after close the browser window? [duplicate]

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

like image 386
Darkry Avatar asked Jul 29 '11 06:07

Darkry


2 Answers

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.

like image 54
jfriend00 Avatar answered Oct 15 '22 00:10

jfriend00


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".

like image 32
Oliver M Grech Avatar answered Oct 15 '22 00:10

Oliver M Grech