2

Is it possible to write onbeforeunload function like this?

   window.onbeforeunload = function(evt) { 

            if(return true)
            {
                if(confirm('Are you sure you want to logout'));
                {
                     //logout function
                }
            }
    }

If user click on the "leave this page" button I want to ask user to logout the session. Is it possible?

CJ Ramki
  • 2,620
  • 3
  • 25
  • 47

1 Answers1

0

Is this what You need?

   <script type="text/javascript">
      window.onbeforeunload = function() { 
       if(confirm('Are you sure you want to logout'))
       {
            alert("logout")
       }
    }
  </script>
Hanky Panky
  • 46,730
  • 8
  • 72
  • 95
Gowsikan
  • 5,571
  • 8
  • 33
  • 43
  • You can't do that in onbeforeunload. You need to return a string and alert / confirm is not allowed. – putvande Nov 14 '13 at 08:03
  • 1
    @putvande try with the code snippet and tell. This is working for me – Gowsikan Nov 14 '13 at 08:09
  • @Gowsikan It is working. If i click on browser back button, If I click cancel in confirm box, It should stay on the same page. But It navigates to previous page. There is any option to stay user on the same page – CJ Ramki Nov 14 '13 at 08:37
  • @CJRamki pl see http://stackoverflow.com/questions/3986430/how-we-call-logout-servlet-on-browser-close-event – Gowsikan Nov 14 '13 at 09:47
  • Thanks guys, I just disable browser back button. I referred this link,(http://programmerslate.blogspot.in/2013/06/disable-back-button-of-your-browser.html?showComment=1384425729990#c5917527548982551705). But I don't know all browsers support this... – CJ Ramki Nov 14 '13 at 10:58