Chrome - Detect When Browser Exits

前端 未结 2 593
囚心锁ツ
囚心锁ツ 2020-12-18 07:03

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 st

相关标签:
2条回答
  • 2020-12-18 07:44

    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

    0 讨论(0)
  • 2020-12-18 07:49

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

    http://code.google.com/chrome/extensions/windows.html#event-onRemoved

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

    http://code.google.com/chrome/extensions/tabs.html#event-onRemoved

    API Index

    0 讨论(0)
提交回复
热议问题