Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Mubashar007's avatar

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.

0 likes
20 replies
Mubashar007's avatar

@Armani it did not work exactly what I want to do. Please let me know another way to do this....!

Sinnbeck's avatar

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

Mubashar007's avatar

@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's avatar

@Sinnbeck ok, so is there any other way to manipulate the data into the database during closing the browser closing?

Mubashar007's avatar

@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.

Snapey's avatar

@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?

Sinnbeck's avatar

@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

Mubashar007's avatar

@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.

Sinnbeck's avatar

@Mubashar007 ah ok so closing the browser count as logging out but the session timing out does not.

Mubashar007's avatar

@sinnbeck @snapey Also the problem is that I can't use the expire_on_close function of laravel because I need to change the authenticated flag in the Database when sessions are destroyed.

Please or to participate in this conversation.