有点问题。我希望每当浏览器或选项卡关闭时执行查询,但如果该页面中存在任何按钮或链接,并且我点击它们中的任何一个,那么查询也会执行。 。需要帮助
First.php
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1
/jquery.min.js"></script>
<script type="text/javascript">
$(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;
});
window.addEventListener("beforeunload", function (e) {
if (!validNavigation) {
$.ajax({
url: 'logout.php',
type: 'POST',
async: false,
timeout: 4000
});
}
})
</script>
logout.php
<?php
$username = "root";
$password = "";
$hostname = "localhost";
$dbhandle = mysql_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
$selected = mysql_select_db("examples",$dbhandle)
or die("Could not select examples");
$result = mysql_query("DELETE FROM cars WHERE id='1'");
mysql_close($dbhandle);
?>
答案 0 :(得分:0)
事件&#34; beforeunload&#34;浏览器卸载当前文档时触发。如果您在同一页面上使用表单,则需要在按下提交时取消设置该事件。
例如像这样:
window.onbeforeunload=null;
你可以在onsubmit处理程序中这样做:
<form onsubmit="cancelUnloadAndSubmit()">
Enter name: <input type="text">
<input type="submit">
</form>
答案 1 :(得分:0)
像这样包装点击处理程序:
@ApplicationScoped