2012-04-22 212 views
0

我正在測試一個基於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="&quot;You can put a caption for your image right here&quot;"></li> 
    <li><img width="950" height="450" src="images/home_2.jpg" alt="&quot;You can put a description of the image here if you like, or anything else if you want.&quot;"></li> 
    <li><img width="950" height="450" src="images/home_3.jpg" alt="&quot;You can put a description of the image here if you like, or anything else if you want.&quot;"></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> 

問題是什麼?

+1

你爲什麼要用JavaScript而不是在服務器上做到這一點。似乎是一個非常糟糕的主意。 – epascarello 2012-04-22 13:46:39

+0

什麼是Javascript函數'endsWith'? – iambriansreed 2012-04-22 13:51:40

+0

你有一個庫,添加字符串方法'.endsWith()',因爲這不是一個標準的方法? – jfriend00 2012-04-22 13:52:09

回答

1

沒有.hidden屬性。它是元素風格的一部分,您可以使用可見性。

document.getElementById("LogoutMessage").style.visibility = "hidden"; // or "visible" 

如果我猜,你想用display而不是hidden

document.getElementById("LogoutMessage").style.display = "none"; // or "block" 
+2

不應該是document.getElementById(「 LogoutMessage「)。style.visibility ='hidden' – anuragsn7 2012-04-22 13:44:02

+1

@ anuragsn7抓住我的編輯!早晨的咖啡沒有踢。:) – epascarello 2012-04-22 13:45:33

+0

爲什麼函數沒有被調用? – 2012-04-22 13:53:47

1

爲了有一個叫做你的代碼,無論是做:

<body onload="urlCheck()">

或把腳本在body標籤的盡頭,只有你的函數的內碼。

0

Javascript沒有本地endsWith。所以,你必須先創建一個

String.prototype.endsWith = function(suffix) { 
    return this.indexOf(suffix, this.length - suffix.length) !== -1; 
}; 

而且,沒有 style.hidden property 。請使用style.display代替

Thecorrect功能urlCheck應該是這樣的

function urlCheck() { 
    alert("in the function urlCheck"); 
    if(document.URL.endsWith("logout.jsp")) 
     document.getElementById("LogoutMessage").style.display= 'block'; 
    else if(document.URL.endsWith("index.jsp")) 
     document.getElementById("LogoutMessage").style.display= 'none'; 
} 

哦,如麥克風說請撥打功能的onload這樣。

window.onload = function() { urlCheck(); } 
+0

我認爲Suhail希望在鏈接中顯示'logout.jsp'消息。 – 2012-04-22 15:12:38

+0

oops ..錯字.. :) – naveen 2012-04-22 22:35:55

0

您可以使用jQuery。包括

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script> 

,並使用

$('#LogoutMessage').hide(); 

隱藏的消息,並

$('#LogoutMessage').show(); 

用於顯示信息

要調用你的函數

$(document).ready(function(){ 
    urlCheck(); 
}) 
function urlCheck() { 
    var url = document.URL; 
    var urlArr = url.split('/') 
    if($.inArray('logout.jsp', urlArr)) 
    { 
     $('#LogoutMessage').show(); 
    } 
    if($.inArray('index.jsp', urlArr)) 
    { 
     $('#LogoutMessage').hide(); 
    } 
} 

嘗試以上操作。

相關問題