简体   繁体   English

如何使用Javascript检测移动浏览器(Android Chrome)的关闭?

[英]How to detect closing of mobile browser (Android Chrome) with Javascript?

we are trying to get some thorough data on our users' activity, so we are building ourselves a library, that logs time users spend on given page. 我们正在尝试获取有关用户活动的全面数据,因此我们正在构建一个库,用于记录用户在给定页面上花费的时间。 We are measuring times using TimeMe library. 我们使用TimeMe库测量时间。 We are trying to send the measurments to our server only once, when user change page, quit tab, close browser etc. 当用户更改页面,退出选项卡,关闭浏览器等时,我们只尝试将测量值发送到我们的服务器一次。

Currently we are listening to two events: 目前我们正在听两个事件:

  1. page:before-unload on document (because of rails' turbolinks) page:before-unload on document(因为rails的turbolinks)
  2. beforeunload on window (other cases) 在窗口上的前载(其他情况)

It works just fine on desktop browsers. 它适用于桌面浏览器。 However these listeners won't catch some events. 然而,这些听众不会发现一些事件。 For example if we visit the site on mobile browser (Android Chrome) and then close this browser, these listeners won't fire. 例如,如果我们在移动浏览器(Android Chrome)上访问该网站然后关闭此浏览器,则这些侦听器将不会触发。

My question is, how can we fix this? 我的问题是,我们如何解决这个问题? What event do mobile browser create, when they are being closed? 移动浏览器在关闭时会创建什么事件?

Thank you for your advice. 感谢您的意见。

You can never detect the closing of browser by JavaScript, JavaScript is a Dynamically typed language and it uses browser engines to execute. 您永远无法通过JavaScript检测到浏览器的关闭,JavaScript是一种动态类型语言,它使用浏览器引擎来执行。 But if you are going to close browser then how can the JavaScript code will be able to execute. 但是,如果您要关闭浏览器,那么JavaScript代码将如何能够执行。 So, this is not possible at all. 所以,这根本不可能。 but in cases to deal with google chrome, since android is an operating system and supports javascript compilation, because V8 is also the execution engine for chrome provided by google. 但在处理谷歌浏览器的情况下,由于android是一个操作系统并支持javascript编译,因为V8也是谷歌提供的Chrome执行引擎。 So the following script might lead to a right track. 因此,以下脚本可能会导致正确的轨道。

$(window).bind('beforeunload', function() { $(window).bind('beforeunload',function(){
if (iWantTo) { 如果我想) {
return "Don't leave me!"; 返回“别离开我!”;
} }
}); });

Blockquote

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 通过Mobile Safari中的JavaScript检测关闭浏览器选项卡? - Detect closing browser tab via JavaScript in the Mobile Safari? 如何检测浏览器关闭? - How to detect browser closing? 如何在网站中使用 jquery/javascript 检测从 web 浏览器(chrome)或 webview(移动应用程序)打开的 url? - How to detect the url opening from web browser(chrome) or webview(mobile app) using jquery/javascript in the website? 如何使用 javascript 或 html 打开移动 chrome 浏览器 - how to open mobile chrome browser with javascript or html 如何在 javascript 中检测浏览器 window 是否正在关闭(不重新加载,仅关闭) - How to detect if the browser window is closing (not reload, only close) in javascript 如何在Django中检测浏览器的关闭? - How to detect closing of a browser in Django? 如何在chrome-extension应用程序中禁用Chrome浏览器的javascript - How to detect javascript of chrome browser is disabled in chrome-extension application 检测移动浏览器和javascript支持 - detect mobile browser and javascript support 使用 Javascript 检测桌面浏览器(非移动设备) - Detect Desktop Browser (not mobile) with Javascript 如何检测浏览器的chrome - How to detect browser for chrome
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM