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"
});
}
});
}