[英]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: 目前我们正在听两个事件:
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.