3

User is able to delete own publication, therefore publication is hidden and ajax request to delete the post is sent to server. But I want make it possible to recover post until user is on that page, so ajax request must be sent only when user closes the page? Is it possible to implement such a thing or the only way would be to copy and store that publication in JS in order to recover if necessary?

  • 2
    The is an unload event for the window that you can bind to, but ajax is asynchronous, so there's some weirdness there with making sure the request finishes before the page is left. Not sure if that is the best approach. – Taplar Dec 08 '16 at 23:55
  • 1
    Yes that ^^. The event is `onbeforeunload`. But as @Taplar said, what if it doesn't finish before the page leaves? – TheValyreanGroup Dec 08 '16 at 23:56
  • @TheValyreanGroup how could there be any mistake so that request is not accomplished ? Just id of publication passed to POST and few lines on server side –  Dec 08 '16 at 23:57
  • 1
    There's no way to do this reliably. Instead of deleting the post when the user leaves the page, use a cron job on the server that deletes posts after they have been marked deleted for a period of time. – Barmar Dec 08 '16 at 23:59
  • 1
    good browsers have [window.sendBeacon](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon) - so, not IE or Safari (because I stated **good** browsers) ... no support in Android browser (Chrome OK) IE mobile, safari mobile or opera mobile either – Jaromanda X Dec 09 '16 at 00:01
  • 1
    @Vinand, just because it's only a few lines of server code doesn't mean it will complete. You'll be sending the POST to la la land _hoping_ it works. You should always confirm your request has been received. – TheValyreanGroup Dec 09 '16 at 00:03
  • This question was asked earlier today. The solution was to set `XMLHttpRequest` `async` to `false`. – guest271314 Dec 09 '16 at 00:03
  • do you have a link @guest271314 ? does the other question have an answer? – Jaromanda X Dec 09 '16 at 00:04
  • Will have to find link. The question today was marked as a duplicate. Yes, the duplicate Question has an accepted Answer. – guest271314 Dec 09 '16 at 00:05
  • @JaromandaX Here is one link, though not the same one linked to earlier http://stackoverflow.com/questions/4945932/window-onbeforeunload-ajax-request-problem-with-chrome/20322988#20322988 – guest271314 Dec 09 '16 at 00:09
  • @JaromandaX This is the link http://stackoverflow.com/questions/653976/call-url-before-closing-of-browser-window from http://stackoverflow.com/questions/41033630/call-an-api-from-angularjs-with-promise-async-falseon-browser-close-event – guest271314 Dec 09 '16 at 00:13
  • sendBeacon isn't mentioned at all – Jaromanda X Dec 09 '16 at 00:26

1 Answers1

1

The ideal and most common way to do this is to set a "deleted" flag in the posts table rather than actually deleting it. This is how Facebook, StackOverflow and many other do it. Nothing is actually deleted anymore.

That said, if you still want to do it your way you can send an ajax request that doesn't wait for a response.

$(window).unload(function(){
    $.ajax({url: "myurl.php"});
});

On the back end you can then close the connection as soon as it's recieved and keep processing. If you're working with PHP you can do something like this:

<?php
// Close connection
header("Connection: close");
ob_start();
echo "Closed";
header("Content-Length: ".ob_get_length());
ob_end_flush();
flush();

// connection is closed, upload stuff to db or whatever...

This is not reliable or ideal and to implement any kind of "undelete" feature, setting a flag in the DB is proper and only reliable way to do this.

I wrestled a bear once.
  • 22,983
  • 19
  • 69
  • 116