Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XMLHttpRequest onunload?

In my web app, I need to send the latest data the user has changed before they leave the page.

I call up a function like this when the page unloads:

window.onbeforeunload=sendData;

That's what's inside the function called

function sendData(){
        var xhr = new XMLHttpRequest;
        var storage = container;
        xhr.open("POST","save.php",false);
        xhr.send("information="+container);
}

My questions:

  1. What is more right: Using async or sync to make send the requests before the user closes the page?

  2. Is it possible to make the requests smaller? I only send variables containing up to two characters and the whole request takes 171 bytes!

like image 992
user1431627 Avatar asked Mar 24 '26 05:03

user1431627


1 Answers

It's necessary to use a synchronous request, otherwise, the data is not transmitted in IE10 and IE11, see Unload event in IE10, no form data.

like image 190
haui Avatar answered Mar 25 '26 19:03

haui



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!