screen_rotation
Copied to Clipboard
<html> <head> <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script> </head> <body> <h3>Vue Js Count User switches tabs or leaves the page</h3> <div id="app"> <p>You left the page {{ count }} times.</p> </div> <script type="module"> const app = new Vue({ el: "#app", data() { return { count: 0 }; }, created() { document.addEventListener('visibilitychange', this.handleVisibilityChange); }, destroyed() { document.removeEventListener('visibilitychange', this.handleVisibilityChange); }, methods: { handleVisibilityChange() { if (document.visibilityState === 'hidden') { this.count++; } } } }); </script> </body> </html>