我正在测试一个基于URL的东西。如果URL以logout.jsp结尾,则应该显示注销消息 ,并且如果URL以index.jsp结束,则不应该显示该消息。为此,我使用了 document.getElementById("id").hidden = value
。但是这不起作用。事实上这个函数没有被调用。 我不知道问题是什么。为什么函数没有被调用?
HTML片段
<table id="LogoutMessage" onload="urlCheck()">
<tr>
<td>
<h2>You are successfully logged out !</h2>
</td>
</tr>
</table>
javascript函数
<script type="text/javascript">
function urlCheck() {
alert("in the function urlCheck");
if(document.URL.endsWith() = "logout.jsp")
document.getElementById("LogoutMessage").hidden = false;
else if(document.URL.endsWith() = "index.jsp")
document.getElementById("LogoutMessage").hidden = true;
}
</script>
全页面在JSP
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>PhotoArtWork</title>
<jsp:include page="reusable/stylesheets.html" />
<script type="text/javascript" src="js/modernizr-1.5.min.js"></script>
</head>
<body>
<jsp:include page="reusable/header.html" />
<!-- begin content --><div id="site_content">
<table id="LogoutMessage" onload="urlCheck()">
<tr>
<td>
<h2>You are successfully logged out !</h2>
</td>
</tr>
</table>
<ul class="slideshow">
<li class="show"><img width="950" height="450" src="images/home_1.jpg" alt=""You can put a caption for your image right here""></li>
<li><img width="950" height="450" src="images/home_2.jpg" alt=""You can put a description of the image here if you like, or anything else if you want.""></li>
<li><img width="950" height="450" src="images/home_3.jpg" alt=""You can put a description of the image here if you like, or anything else if you want.""></li>
</ul>
</div>
<!-- end content -->
<script type="text/javascript">
function urlCheck() {
alert("in the function urlCheck");
if(document.URL.endsWith() = "logout.jsp")
document.getElementById("LogoutMessage").hidden = false;
else if(document.URL.endsWith() = "index.jsp")
document.getElementById("LogoutMessage").hidden = true;
}
</script>
<jsp:include page="reusable/footer.html" />
</body>
</html>
问题是什么?
你为什么要用JavaScript而不是在服务器上做到这一点。似乎是一个非常糟糕的主意。 – epascarello 2012-04-22 13:46:39
什么是Javascript函数'endsWith'? – iambriansreed 2012-04-22 13:51:40
你有一个库,添加字符串方法'.endsWith()',因为这不是一个标准的方法? – jfriend00 2012-04-22 13:52:09