Click here to Skip to main content
15,037,009 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I am developing a web app where i want to call my javascript Logout() function only when browser tab / browser close button is clicked.

Not when page navigation, page refresh.

I have used onbefeunload. but it is not working properly.

I am doing this enhancement because, when i close the browser tab during login session, i cannot able to logout. But working fine on complete browser close.

1 solution

Hi Dude,

Try this code..

add jquery reference to it..

<script type="text/javascript">

        $(function () {
            $(window).bind("beforeunload", function () {


        function fnLogOut() { alert('browser closing'); }

Member 10284541 28-Nov-13 7:19am
It did not work...:(
Karthik_Mahalingam 28-Nov-13 8:11am
did u add jquery reference to it ???
Member 10284541 29-Nov-13 0:42am
Hi Karthik thank you for your answer..
I have modified the code lilbit as under.

$(function ()
$(window).bind("beforeunload", function ()
function fnLogOut()

ViewerLoging.aspx is my Login Page.
Now its working f9 for me in firefox but not in chrome.

give me any idea.
Karthik_Mahalingam 29-Nov-13 2:44am

i got this code from other portal , this might help you i guess..

<script type=\"text/javascript\">
var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
var leave_message = 'You sure you want to leave?'
function goodbye(e)
if(!e) e = window.event;
//e.cancelBubble is supported by IE - this will kill the bubbling process.
e.cancelBubble = true;
e.returnValue = leave_message;
//e.stopPropagation works in Firefox.
if (e.stopPropagation)

//return works for Chrome and Safari
return leave_message;
Member 11028672 16-Oct-14 3:10am
Member 10284541 2-Dec-13 4:44am
Hi Karthik, Can you please simply suggest me one thing.

I want to clear all the Login sessions when user clicks on browser close button without Logout.
Now its working when user closes the whole browser but not not working on tab close.
Karthik_Mahalingam 2-Dec-13 4:55am
is tab close and browser close different??

event is not firing ??
Member 10284541 3-Dec-13 2:17am
Hi Karthik,

First of all I want to work this app for Google chrome and safari.

I want to call a function on tab close not on page navigation.

So please help me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900