user2623946
user2623946

Reputation: 55

Invalidate session and close browser tab on click of Logout

Below is my code to call a servlet that invalidates user session and closes browser tab . In IE , the browser asks the user for confirmation to close the tab . In Chrome , it throws an error saying Scripts may close only the windows that were opened by it . I wrote this code based on the feedback that was given to similar questions on stackoverflow . Is there a way to call my logout servlet and close the browser tab subsequently on click of logout without asking for user confirmation.

<!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $("#mybutton").click(function(){
            $.get("/logoutServlet", function(data, status){
               // alert("Data: " + data + "\nStatus: " + status);
            });
    window.open('','_self').close();
        });
    });
    </script>
    </head>
    <body>

    <button id="mybutton">Logout</button>

    </body>
    </html>

Upvotes: 0

Views: 1739

Answers (1)

xerq
xerq

Reputation: 2792

You simply can't. But what you can do instead is redirect the user to a blank page.

window.location.href = "about:blank";

Upvotes: 1

Related Questions