I need to make an Ajax call when a user leaves my page.
I don't need to wait for the end of the call, I just need to notify my server with a kindly "hey, user XXX is leaving the page", without notifying the client.
Here is what I've done so far :
window.onbeforeunload = function () {
$.ajax({
type: "POST",
url: myURL,
contentType: "application/json; charset=utf-8",
data: JSON.stringify({ xxx: xxx, yyy: yyy })
});
}
This perfectly works with Chrome and Edge, but this event is not raised on Firefox.
What I've done so far :
- I tried this SO answer, as the author claims it works and has a good score, but once again, my ajax call is not fired.
- Add
async: false
without any success - Also tried to use
beforeunload
instead ofonbeforeunload
Can anyone explain me how to fire my Ajax call when an user leaves a page, no matter he uses Chrome, Edge or Firefox ? Thanks in advance ?
- Oct 17, 2023 10:41 IST
answer 1
How can you tell it isn't working?
In general, there's little time between beforeunload event, unload event and actual page exit. At page unload all running scripts are dropped (browser than closes the window or navigates to address provided by user for example).
What might be happening here is browser doesn't really have time to send ajax request before page is unloaded.
I've seen couple of ways to ensure your final request before page unload will be completed. One of them is sending request and then introducing loop that is running for X number of miliseconds, postponing unload event and ensuring ajax request can be completed.
- Nov 07, 2023 23:23 IST
Table update
S.No प्लेयर मैच ओवर विकेट औसत 4-विकेट्स 5- विकेट्स 1 Dilshan Madushanka 8 72 21 22.24 1 1 2 Adam Zampa 8 69 20 19.2 3 - 3 Marco Jansen 8 64.4 17 24.41 - - 4 Mohammed Shami 4 26 16 7 1 2 5 Shaheen Afridi 8 71 16 25.56 - 1 *Last Updated on 07 Nov, 2023 after the match between Australia and Afghanistan
- Nov 02, 2023 13:45 IST
add new update
- hello
- ufldsfjlskf
- dfjdklsajf;dsf
- dsajfklsadjfas
- Oct 26, 2023 16:57 IST
new update
new update
- Oct 26, 2023 16:57 IST
fdsafdsf
safsdfsdafsd
- Oct 19, 2023 14:18 IST
fsadfdf
sfdsafdsa
- Oct 19, 2023 14:17 IST
gdsgfds
sdfgsfgsd
- Oct 19, 2023 14:11 IST
fdsafsa
adsfdasf
- Oct 19, 2023 14:09 IST
fdsafdsa
safd
- Oct 19, 2023 14:06 IST
new update
new update
- Oct 19, 2023 14:05 IST
fadsfdsfdsfadsf
sdafdsfacvzcxvxz
- Oct 19, 2023 13:33 IST
dafadsfadsf
fadsfcvcxbcxvbxc
- Oct 19, 2023 13:28 IST
fdsafdsaeqrqwrqw
vxczvzcxvcxzvwerqrqrr
- Oct 19, 2023 13:25 IST
fdsf
fdasf
- Oct 19, 2023 13:24 IST
vzxcvczxv
vzcxvzcvxz
- Oct 19, 2023 13:13 IST
cccc
ccccccccc
- Oct 19, 2023 12:57 IST
fdsafdsa
afsdafsa
- Oct 19, 2023 12:56 IST
new update with if
new update with if
- Oct 18, 2023 17:29 IST
fds
as
- Oct 18, 2023 14:47 IST
dfgsdgfsd
dsfgfsdgsdgsg
- Oct 18, 2023 14:46 IST
vcbx
cvbxc
- Oct 18, 2023 14:45 IST
zzzzzzzzzzzzfdsfsabcvbcvb
dsfadscvxcvc
- Oct 18, 2023 14:42 IST
Add New Update
Add New Update
- Oct 18, 2023 14:42 IST
vcxvcxbvcx
cxvbxvcbxvcbxc
- Oct 18, 2023 14:41 IST
fadsfdsaf
fasdfsafas
- Oct 18, 2023 14:40 IST
amp-live-list testing 3
amp-live-list testing 3
- Oct 18, 2023 14:35 IST
amp-live-list testing 2
amp-live-list testing 2
- Oct 18, 2023 14:35 IST
amp-live-list testing
amp-live-list testing
- Oct 18, 2023 14:33 IST
new update 19
new update 19
- Oct 18, 2023 14:32 IST
new update 18
new update 18
- Oct 18, 2023 14:26 IST
new update 16
new update 16
- Oct 18, 2023 14:26 IST
new update 16
new update 16
- Oct 18, 2023 14:25 IST
new update 15
new update 15
- Oct 18, 2023 14:25 IST
new update 15
new update 15
- Oct 17, 2023 10:48 IST
update 13
update 13
- Oct 17, 2023 10:48 IST
update 12
update 12
- Oct 17, 2023 10:48 IST
update 11
update 11
- Oct 17, 2023 10:48 IST
update 10
update 10
- Oct 17, 2023 10:48 IST
update 9
update 9
- Oct 17, 2023 10:48 IST
update 8
update 8
- Oct 17, 2023 10:47 IST
update 7
update 7
- Oct 17, 2023 10:47 IST
update 6
update 6
- Oct 17, 2023 10:47 IST
update 5
update 5
- Oct 17, 2023 10:47 IST
update 4
update 4
- Oct 17, 2023 10:47 IST
update three
update three
- Oct 17, 2023 10:41 IST
second update
The problem is that you use a GET instead of a POST request. The browser may use a cached result from the very first request.
This explains the fact that "it works only on the first time I open IE" as written as in response to another answer.
By the way, the AJAX call in
onunload
seems to work reliably in IE10 only if you use the parameterasync = false
inXMLHttpRequest.open(...)
, which you already did.