Do action if reload or refresh window after onclick event

After Onclick event set a flag in Database and then another click unset the flag .
But my question is what if, someone set flag after click and refresh or reload window instead another click.
I tried to write piece of code but it seems not working .
Might be my problem is simple but couldn’t get it …

function flagSet(){
        $.ajax({
            url: url_api + '?c=do_flag_set&module=flag',
            type: "POST",
            data: {
                c       : 'do_flag_set',
                module  : 'flag',
                ID      :  'ID'
            },
            dataType: "json"
        });
    
       // If somehow reload or refresh page the flag shoud unset 
	   
        var getEvent = window.attachEvent || window.addEventListener;
        var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; 

         getEvent(chkevent, function(e) { 
            var confirmMessage= ' '; 
            (e || window.event).returnValue = confirmMessage;
            return confirmMessage;
    // after showing reload / refresh message page reloaded but unset part is not working 
           
// condition part is not working after reload/refresh page
 if ( confirmMessage) {    
                $.ajax({
                     url: url_api + '?c=do_flag_unset&module=flag',
                     type: "POST",
                     data: {
                        c           : 'do_flag_unset',
                        module  : 'flag',
                        ID         : 'ID'
                    },
                    dataType: "json"
                }); 
            }
        });   
    }

beforeunload is notoriously flaky. Why not check for the flag on page load and unset it if it is present?

The think is only one person can reply at a time .I just set flag when anyone popup the reply button and the same time another get alert message.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.