我正在測試一個基於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