2012-04-22 21 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> 

JSPで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> 

ページ全体

<%@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

+0

Javascript関数 'endsWith'とは何ですか? – iambriansreed

+0

文字列メソッド '.endsWith()'を標準メソッドではないので追加しているライブラリはありますか? – jfriend00

答えて

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

+1

@ anuragsn7私の編集でキャッチ!朝のコーヒーは蹴られていません:) – epascarello

+0

なぜ呼び出されていないのですか? –

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 はありません。代わりに

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'; 
} 

ああのようにする必要がありますstyle.displayを使用してくださいとマイクが言うように、このような関数のonloadイベントにご連絡ください。

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

リンクに 'logout.jsp'がある場合、Suhailはメッセージを表示したいと思います。 –

+0

oops .. typo .. :) – naveen

0

jQueryを使用できます。

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

とは

は、あなたの関数を呼び出すには、メッセージを表示するためのメッセージと

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

を隠すため

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

を使用含める

$(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(); 
    } 
} 

上記を試してください。

関連する問題