user1431627
user1431627

Reputation: 815

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!

Upvotes: 3

Views: 1553

Answers (1)

haui
haui

Reputation: 617

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.

Upvotes: 1

Related Questions