Javascript 当选项卡或浏览器窗口关闭时注销
我是新的@jsp,希望在选项卡或浏览器窗口关闭时调用注销函数。我做了一些研究,发现我需要使用window.onbeforeunload。但问题是,我不知道把它放在我的jsp页面的什么地方。这是它的代码:Javascript 当选项卡或浏览器窗口关闭时注销,javascript,html,jsp,Javascript,Html,Jsp,我是新的@jsp,希望在选项卡或浏览器窗口关闭时调用注销函数。我做了一些研究,发现我需要使用window.onbeforeunload。但问题是,我不知道把它放在我的jsp页面的什么地方。这是它的代码: <%@ page language="java" contentType="text/html; charset=windows-1256" pageEncoding="windows-1256"%> <!DOCTYPE html PUBLIC "-//W3C//DTD
<%@ page language="java" contentType="text/html; charset=windows-1256"
pageEncoding="windows-1256"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1256">
<%@ taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jstl/fmt_rt" prefix="fmt" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib uri="http://java.sun.com/jstl/core-rt" prefix="c-rt" %>
<link rel="stylesheet" href="css/login.css">
<title>Insert title here</title>
<script type="text/javascript" language="javascript" runat="server">
var divSelected = null;
function switchURL()
{
var x = document.getElementById("femp").value;
<c:choose>
<c:when test="${puser.LOGIN==true}">
document.getElementById("temp").value = x;
// alert("${puser.LOGIN}");
</c:when>
<c:otherwise>
document.getElementById("temp").value = 0;
document.getElementById("femp").value = 0;
// alert("${puser.LOGIN}");
</c:otherwise>
</c:choose>
}
</script>
<script type="text/javascript" language="javascript" runat="server">
var divSelected = null;
function Logout()
{
document.getElementById("Form1").target ="";
document.getElementById("Form1").action = "/dcHR/login";
// window.location = "login";
}
</script>
</head>
<body>
<section class="container">
<div class="login">
<form id="Form1" action="http://localhost:80/reports/rwservlet" method="Post" target="_blank">
<input type="hidden" id="cmdkey" name="cmdkey" value="repserv">
<input type="hidden" id="temp" name="P_TO_EMP" value="0">
<input type="hidden" id="femp" name="P_FROM_EMP" value="${puser.USR_USER}" >
<input type="hidden" id="pcomp" name="P_COMP" value="${puser.USR_CP}">
<p> Years:<input type="text" name="P_YEAR"> </p>
<p> Month:<input type="text" name="P_PERIOD"> </p>
<input type="submit" value="Report" name="pbtn" onclick="switchURL()" />
<input type="submit" value="Logout" name="pbtn" onclick="Logout()"/>
</form>
</div>
</section>
</body>
</html>
在此处插入标题
var divSelected=null;
函数switchURL()
{
var x=document.getElementById(“femp”).value;
document.getElementById(“temp”).value=x;
//警报(${puser.LOGIN});
document.getElementById(“temp”).value=0;
document.getElementById(“femp”).value=0;
//警报(${puser.LOGIN});
}
var divSelected=null;
函数注销()
{
document.getElementById(“Form1”).target=“”;
document.getElementById(“Form1”).action=“/dcHR/login”;
//window.location=“login”;
}
年份:
月份:
有人能帮我吗
关于所有对象。您必须在窗口对象上添加侦听器,如下所示
window.addEventListener("beforeunload", function(){
//Ajax call to invoke logout action
});
将此代码放在jsp页面中。使用
onbeforeunload
的概念并不实用,因为它不会触发的原因有很多……用户电源故障、没有internet连接、浏览器崩溃etci将其添加到bloc中,但什么也没有发生:var divSelected=null;函数Logout(){document.getElementById(“Form1”).target=“;document.getElementById(“Form1”).action=“/dcHR/login”;}窗口。addEventListener(“beforeunload”,函数(){返回“如果关闭窗口,则应用程序仍在运行。如果要停止应用程序,请单击“取消”并”;});在beforeunload事件处理程序中添加Logout()函数调用,如下所示。addEventListener(“beforeunload”,function(){Logout();}”);来自用户的确认将由浏览器处理,因为在此事件中窗口的警报、确认和提示方法将被忽略。问题是,我不确定它是否通过window.addEventListener下的代码。注销功能,重定向e page至/dchr/login,并将target=“”。然后在扩展HttpServlet的类中执行特定代码。我如何能够在不通过重定向至/dchr/login的情况下执行该代码。知道浏览器将已关闭。再次感谢您宝贵的帮助。