How to record when user goes from iframe site

I have a website that is inside an iframe.

How can I detect and record the time that a user moves from my site?

I started to do this:

$(window).unload(function() { $.post('record_action.php'); }); 

but it happened that the ajax function started, but did not end, because the page moved, and the ajax function ended earlier (it connected incorrectly).

+4
source share
1 answer

Since AJAX calls are asynchronous by default , before calling $.post page closes and the result is not sent to your server.

You need to use the synchronous ajax call here to make sure the page closes after sending the message.

 $(window).unload(function() { $.ajax({ url: "record_action.php", async: false, type: 'POST', success: function(){ // } }); }); 
+1
source

Source: https://habr.com/ru/post/1381125/


All Articles