onbeforeunload Function not working properly.
window.onbeforeunload = function () { return "Do you really want to close?"; };
I am calling this function on tab close but apart from getting called on tab close this function also gets called whenever I hit 'a' tag, on every button click, on page refresh and form submit. please help me resolve this issue.
@Armani it did not work exactly what I want to do. Please let me know another way to do this....!
I don't think there is anyway for Javascript to tell if a tab is closed. You can only check if the page is exiting, which is what you are experiencing
@Sinnbeck I think it is possible but we need to search more on the problem.
@Mubashar007 what do you need to do when the tab is closed. And what if I have the site in 3 tabs?
@Sinnbeck I just simply want to logout the user and change the field of a post table to false whenever the user close the tab. using the Ajax call.
@Mubashar007 you can expire the session when the user closes the browser, which logs them out (but does not change anything in the database) https://github.com/laravel/laravel/blob/9.x/config/session.php#L36
But if I have 2 tabs with your site open and close one, and you sign me out, I would stop using your site
@Sinnbeck ok, so is there any other way to manipulate the data into the database during closing the browser closing?
@Mubashar007 this is the closest I can find https://stackoverflow.com/a/26275621
But I don't think I have ever seen a web site with a feature like this so I'm unsure what the use case is. If you just want to show if people are online, you can use websockets
@Sinnbeck we have done with that but now we are facing only a problem with Mac. Mac didn't allow us to make an ajax call at this time and I am sharing the code here with you Please look at this.
var validNavigation = 0;
function endSession() {
logout();
}
function bindDOMEvents() {
/*
* unload works on both closing tab and on refreshing tab.
*/
$(window).unload(function() {
if (validNavigation == 0) {
console.log("i am closed")
endSession();
}
});
$(document).keydown(function(e) {
var key = e.which || e.keyCode;
if (key == 116) {
validNavigation = 1;
}
});
$("form").bind("submit", function() {
validNavigation = 1;
});
$("input[type=submit]").bind("click", function() {
validNavigation = 1;
});
}
$(document).ready(function() {
$(window).bind('click', function(event) {
if (event.target.href) {
$(window).unbind('beforeunload');
}
});
bindDOMEvents();
});
function logout() {
$.ajax({
heeader: {
"X-CSRF-TOKEN": "{{ csrf_token() }}"
},
type: 'GET',
data: user_id,
url: session_out_url,
asyn: true,
success: function(response) {
return true;
},
});
}
I am sure it's 100% working in windows you are logged out when you close the browser tab.
@Mubashar007 sorry but I don't have a Mac
@Sinnbeck it's okhy
@Mubashar007 its likely to be browser specific not operating system. When you say 'Windows' and 'Mac' what browsers are you actually talking about? Works in Chrome and not in Safari?
Be aware that there is no good way to detect the end of the user's session.
If I just close the lid on my laptop for 3 days, what does that count as?
@Mubashar007 I agree with @snapey . There are soo many edge cases. What if I just leave my computer on over the weekend at the session is timed out. Did I logout?
I suggested earlier that perhaps you can explain the use case. My suggestion was websockets, but without knowing what you need it for, it is hard to give ideas
@Snapey you are absolutely right it's browser-specific, not operating-system-specific but the problem is that safari did not call ajax request. if you close your lid your sessions will be out after 30 minutes with laravel sessions lifetime.
@Mubashar007 ah ok so closing the browser count as logging out but the session timing out does not.
@Sinnbeck yes and also need to manipulate the key into database
@Mubashar007 but not when the session times out?
@Sinnbeck Then continue the user to perform his task.
Please or to participate in this conversation.