Click here to Skip to main content
15,072,433 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want user logout automatically from any page in website if browser is closed ..?

i hope you understant my question . here is my code

i want to call logout button please how can i call

-----------------Java Script Code

XML
<script type="text/javascript">
       function check() {
           if(window.closed)
           {
           aler("Please Save Data First");
           }
           }


       }
     </script>



<----------calling>


XML
<body  style="width: 100%; height: 100%"  bgcolor="#333333" onunload="check();">
Posted

1 solution

you can do it in two ways ,
1) You can call Logout button click event using javascript using.

HTML
<script type="text/javascript">
       function check() {
           if(window.closed)
           {
             document.getElementById("btnLogout").click();
           }
        }
 

       }
     </script>


where btnLogout is id of logout button

2) you can execute code for logout using pageMethod or Ajax method

HTML
<script type="text/javascript">
       function check() {
           if(window.closed)
           {
          $.ajax({
                type: "POST",
                url: "Default.aspx/Logout", // PageName/MethodName for Logout
                contentType: "application/json;charset=utf-8",
                data: {},
                dataType: "json",
                success: function (data) {

                    alert("logout Successfully..!!");
                   

                },
                error: function (result) {
                    alert("Error");

                }
            });

           }
        }
 

       
     </script>


c#

C#
[WebMethod]
       public static void Logout()
       {
       //Your c# script for logout..

       }
   
v2

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