【发布时间】:2013-12-03 05:07:22
【问题描述】:
我想在按下 alt+tab 或 windows+d 时触发一个事件。以下是我在鼠标指针远离浏览器窗口时发出警报的代码。但即使用户按下 alt+tab 或 Windows+D 也应该发生此事件。任何人都可以在这方面帮助我吗?以下是我的代码供您参考:
<!DOCTYPE html>
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
<script>
var timer;
$(document).ready(function () {
$(document).mouseleave(function () {
//alert("Mouse is away");
customAlert("your mouse is away");
});
});
function customAlert(customText) {
$("#popUp").html(customText);
timer = setInterval(customAlert2, 5000);
$("#popUp").dialog({
dialogClass: "no-close",
buttons: [{
text: "OK", click: function () {
$(this).dialog("close");
clearInterval(timer);
}
}]
});
}
function customAlert2() {
location.reload();
$("#popUp2").dialog({
dialogClass: "no-close",
buttons: [{
text: "OK", click: function () {
$(this).dialog("close");
}
}]
});
}
</script>
</head>
<body>
<h1>My first Javascript program</h1>
<p>Hello World!</p>
<div id="popUp" title="First alert message"></div>
<div id="popUp2" title="Second alert message">Time is over</div>
</body>
</html>
【问题讨论】:
-
JavaScript 只能“看到”浏览器中发生的事情。
-
@PHPLover,你找到答案了吗?
-
我想你可以对 HTML5 Visibility API 感兴趣 davidwalsh.name/page-visibility
标签: javascript jquery