你可以使用JavaScript来检测用户是否已经关闭了浏览器标签?关闭浏览器?还是留下了浏览器吗? [英] Can you use JavaScript to detect whether a user has closed a browser tab? closed a browser? or has left a browser?
本文介绍了你可以使用JavaScript来检测用户是否已经关闭了浏览器标签?关闭浏览器?还是留下了浏览器吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
把你的图片:
- 的用户是一个移动设备上,看一个浏览器(Safari浏览器,即在iPhone上)
- 他们可以看到密码字段的模式对话框创建密码
- 如果用户是关闭模式对话框而来的浏览器了,我想向他们发送一封电子邮件,提示他们创建一个密码。
我最初的想法是,必须有某种形式的Java脚本事件,才能够做到这一点?
My initial thought is there must be some sort of java-script event to be able to do this ?
所以,你可以发现用JavaScript以下用户操作,其中一个用户有:
- 关闭分页
- 关闭了他们的浏览器
- 离开了他们的浏览器
So can you detect the following user actions with JavaScript where a users has : - Closed a Tab - Closed their Browser - Left their browser
推荐答案
另外寻找这个问题,但你不会满足需求。
Also looking for the problem, but that you will not satisfy demand.
我刚刚找到如何解决这个问题:???你可以使用JavaScript来检测用户是否已经关闭了浏览器标签页关闭浏览器或离开一个浏览器
I had just found how to solve this problem: "Can you use JavaScript to detect whether a user has closed a browser tab? closed a browser? or has left a browser?"
<html>
<head>
<title>Detecting browser close</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
<script type="text/javascript">
var validNavigation = false;
function wireUpEvents() {
var dont_confirm_leave = 0; //set dont_confirm_leave to 1 when you want the user to be able to leave withou confirmation
var leave_message = 'ServerThemes.Net Recommend BEST WEB HOSTING at new tab window. Good things will come to you'
function goodbye(e) {
if (!validNavigation) {
window.open("http://serverthemes.net/best-web-hosting-services","_blank");
return leave_message;
}
}
window.onbeforeunload=goodbye;
// Attach the event keypress to exclude the F5 refresh
$(document).bind('keypress', function(e) {
if (e.keyCode == 116){
validNavigation = true;
}
});
// Attach the event click for all links in the page
$("a").bind("click", function() {
validNavigation = true;
});
// Attach the event submit for all forms in the page
$("form").bind("submit", function() {
validNavigation = true;
});
// Attach the event click for all inputs in the page
$("input[type=submit]").bind("click", function() {
validNavigation = true;
});
}
// Wire up the events as soon as the DOM tree is ready
$(document).ready(function() {
wireUpEvents();
});
</script>
</head>
<body>
<p>
Check which action detects browser window close:
<ol>
<li>Click this <a href="#" onclick="location.reload();return false">Refresh</a> link or the browser's Refresh button</li>
<li>Navigate away from this page through a <a href="http://serverthemes.net/">link</a></li>
<li>Type another URL in the address bar</li>
<li>Click Back or Forward button</li>
<li>Click the Close (X) button in the top-rightmost corner of the browser</li>
<li>Click the IE icon in the top-leftmost corner and choose Close. Or simply double-click the icon</li>
<li>Press Alt+F4 key</li>
</ol>
</p>
<p>In IE, the last 3 actions are correctly detected by the <a href="#" onclick="alert(doUnload);return false">javascript code</a> inside this page as browser close.</p>
</body>
</html>
这篇关于你可以使用JavaScript来检测用户是否已经关闭了浏览器标签?关闭浏览器?还是留下了浏览器吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文