Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome - Detect When Browser Exits

I am wondering if it is possible to detect whether the user exits the Chrome browser?

EDIT - Sorry, I wasn't being very clear so I'll explain my situation. I am storing some variables in the browser's localstorage. When the user closes the browser, I want to delete some of these variables.

like image 613
Jon Avatar asked Apr 01 '12 02:04

Jon


People also ask

How can we detect when user closes browser?

A tab or window closing in a browser can be detected by using the beforeunload event. This can be used to alert the user in case some data is unsaved on the page, or the user has mistakenly navigated away from the current page by closing the tab or the browser.

How to detect browser tab close event in jQuery?

We can manage this functionality via beforeunload event. To find the browser or tab close event can be identfied by using 'onbeforeunload' event in javascript and in jQuery can use 'beforeunload' event.

How do I close my browser?

A. The procedure for closing all open Chrome browser tabs has changed over the years, but on most recent Android tablets, press down on the “x” on the end of any open tab. Leave your finger on the tab until the Close All Tabs option appears on screen and then select that option to shut down all of the open pages.


2 Answers

Executing some JavaScript before the window is unloaded

You can hook the OnBeforeUnload event of the window

<script type="text/javascript">
    $(window).bind('beforeunload', function() {
        if (iWantTo) {
            return "Don't leave me!";
        }
    }); 
</script>

Using a heartbeat to know when the user has left

Or create a JavaScript timer that pings your sever every XX seconds. When the pings stop, you can assume the user has closed the browser or navigated away.

http://ajaxpatterns.org/archive/Heartbeat.php

like image 164
msigman Avatar answered Nov 15 '22 05:11

msigman


They have lots of good stuff in their documentation. onRemoved of the window object would seem to do it.

https://developer.chrome.com/docs/extensions/reference/windows/#event-onRemoved

Or perhaps you mean tabs. In which case the onRemoved for the tab object would do it.

https://developer.chrome.com/docs/extensions/reference/tabs/#event-onRemoved

API Index

like image 30
mrtsherman Avatar answered Nov 15 '22 05:11

mrtsherman