How to detect browser window close action

Posted By : Kamaldeep Singh | 30-Sep-2015

In my previous blog I have explained how to Prevent Internet Explorer from caching http calls so that each time you can get fresh data from the server. Here is its link: Prevent Internet Explorer from caching http calls

Now in this blog, I will let you know how to detect browser window close action. Now this action may be sometimes required where you want to perform some kind of function on window close. Say you may be required to call Logout function on a secured website. Now this functionality can be done by the following line of javascript code

<html>
    <head>
        <title>Hello</title>
        <script type="text/javascript" language="Javascript">

            function DetectBrowserExit() {
               alert('Here you can execute task you want');
            }

            window.onbeforeunload = function() {
                DetectBrowserExit();
            }

        </script>
    </head>
    <body>
        Close this browser to notice the browser close event
    </body>
</html>

And if we talk about its browser compatibility, I have tested this in following browsers:-

  • Google Chrome
  • Internet Explorer
  • Mozilla Firefox
  • Safari

Thats all for now. 

 

THANKS

 

About Author

Author Image
Kamaldeep Singh

Kamaldeep is a highly skilled Backend Developer specializing in Java, specifically the Spring framework. He also has extensive knowledge of Javascript and associated frameworks such as Node.js and Express. He possesses a deep understanding of the latest technologies and has hands-on experience with Core Java, Spring Boot, Hibernate, Apache Kafka messaging queue, Redis, as well as both relational databases like MySQL and PostgreSQL and non-relational databases like MongoDB. He has made significant contributions to various projects, including Viral Nation, ExamWorks, TNIBRO, Biogas engineering, SecureNow - Web Application, FB Messenger Chatbot, Dialogflow Chatbot, and Catalyst. Kamaldeep's expertise allows him to seamlessly integrate different technologies into applications, highlighting his adaptability and innovative mindset. His practical experience and strong technical skills make him an invaluable asset to any team.

Request for Proposal

Name is required

Comment is required

Sending message..