2012-04-22 215 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(); 
    } 
} 

尝试以上操作。

相关问题