0

my english is not good. sorry.

I want developing a CMS with php. And I want to change the on_desk database column to 1 when the post edit page is opened and the on_desk column to be 0 when exiting same page By doing this while you are in the Edit Post window tab. I want to prevent the same page from opening in a new window.

I used the following solution. But sometimes it doesn't work well. For example, when I cancel the dialog, it does not work well Do you have another solution? For example, the way not to use beforeunload.

Javascript|jQuery:

function clearDesk(){
    $.post('clearOnDesk.php',{
        "id":parseInt($('body').attr('data-pageId')),
    }, function(){
       $('body').attr('data-desk',1)
    });
} 
function setDesk(){
    $.post('setOnDesk.php',{
        "id":parseInt($('body').attr('data-pageId')),
    }, function(){
       $('body').attr('data-desk',2)
    });
}

setDesk();
const beforLoadFunc = function beforLoadFunc () {
   clearDesk();
   if(parseInt($('body').attr('data-desk')) == 1){
       setTimeout(() => {
               window.addEventListener('mousemove', (e) => {
                   setDesk();
               });
       }, 1000);
   }  
}
window.addEventListener('beforeunload', function onBeforeUnload(e) {
   setTimeout(beforLoadFunc, 500);
   const dialogText = 'are you sure?';
   e.returnValue = dialogText;
   return dialogText;
});

clearOnDesk.php

 if($_POST)
 {
    $id = (int)$this->post('id');
    if($id && $id != '')
    {
        $w['id'] = $id;
    } else die("die!");
    $d['on_desk'] = 1;
    $this->db->update('post',$d,$w,["i","i"]);
    //update on_desk to 0 for (post row) in database by (pageId).
    //by PHP Prepared Statements
}

setOnDesk.php

 if($_POST)
 {
    $id = (int)$this->post('id');
    if($id && $id != '')
    {
        $w['id'] = $id;
    } else die("die!");
    $d['on_desk'] = 2;
    $this->db->update('post',$d,$w,["i","i"]);
    // update on_desk to 1 for (poster row) in database by (pageId).
    //by PHP Prepared Statements
 }
ramin
  • 448
  • 4
  • 15
  • 1
    Does this answer your question? [Way to know if user clicked Cancel on a Javascript onbeforeunload Dialog?](https://stackoverflow.com/questions/4650692/way-to-know-if-user-clicked-cancel-on-a-javascript-onbeforeunload-dialog) – Jan Pfeifer Feb 09 '23 at 08:58
  • 1
    Well it can be worked with before unload but the problem is in terms of ajax you need to wait the complete the process and when unload happens or page reloads your connection gets lost and your job gets aborted. You can solve it either by unloading once the job is done or You can use sockets or push and pull model to get this done. – ieatbytes Feb 09 '23 at 08:59
  • @JanPfeifer Do you know any other way to achieve my goal besides using beforeunload? – ramin Feb 09 '23 at 09:00
  • @ieatbytes Thankful. Do you know an example or demo for `sockets` or `push and pull model`? – ramin Feb 09 '23 at 09:04
  • 1
    Take a look at [sendBeacon](https://developer.mozilla.org/en-US/docs/Web/API/Navigator/sendBeacon), I guess that's what you need. – Teemu Feb 09 '23 at 09:05
  • @JanPfeifer I used trust answer in [this link](https://stackoverflow.com/questions/4650692/way-to-know-if-user-clicked-cancel-on-a-javascript-onbeforeunload-dialog), before. But he did not answer for me. The problem seems to be with ajax. – ramin Feb 09 '23 at 09:07
  • 1
    @ramin give it a try to sendBeacon if it works in your case. SendBeacon would be the good option today. – ieatbytes Feb 09 '23 at 09:17
  • @Teemu , @ieatbytes , Should I use `sendBeacon` instead of `ajax`? When I want to use the `sendBeacon` , I have to put it back in the `visibilitychange`, `unload`, `beforeunload`, or `pagehide`. Will the problem still exist? – ramin Feb 09 '23 at 09:28
  • 1
    Depending on your target device, you might need all those events (excluding `unload`). Though these events are standard, whether they're actually implemented depends on the used browser (actually device mostly). `beforeunload` is not implement in mobile browsers, `visibilitychange` has some issues in desktop browsers, and mobile browsers are a mess when a page is put to "sleep" on the background. My advice is, that you wouldn't even try to ask when a user is about to leave, just send the data when you detect them leaving the page. – Teemu Feb 09 '23 at 10:11
  • @Teemu Thankful, <3, How do I know if the user has left the page? What way should I use? – ramin Feb 09 '23 at 10:24
  • 1
    Like said, play with the mentioned events, it's not easy, but I suppose it's possible to detect a user leaving the page. – Teemu Feb 09 '23 at 10:26

0 Answers0