views:

64

answers:

3

Hi,

I have been working on a Java project in which the reports will be generated in HTML, so I am implementing methods for creating these reports. One important functionality is to be able to have as much info as possible in the tables, but still not clutter too much. In other words the details should be available if the user wishes to take a look at them but not necessarily visible by default.

I have done some searching and testing and found an interesting template for hiding/showing content with the use of CSS and javascript, the problem is that when I try the resultant html page the scripts dont work. I am not sure if it's due a problem in Java or in the javascript itself. I have compared the html code that java produces to the source where I found the template, they seem to match pretty well.

Below are bits of my java code that generates the javascript and the content, i would greatly appreciate if anyone can point out the possible reasons for this problem:

//goes to head

private void addShowHideScript() throws IOException{
    StringBuilder sb = new StringBuilder();
    sb.append("<script type=\"text/javascript\" language=\"JavaScript\">\n");
    sb.append("<!--function HideContent(d) {\n");
    sb.append("document.getElementById(d).style.display=\"none\";}\n");

    sb.append("function ShowContent(d) {\n");
    sb.append("document.getElementById(d).style.display=\"block\";}\n");

    sb.append("function ReverseDisplay(d) {\n");
    sb.append("if(document.getElementById(d).style.display==\"none\")\n");
    sb.append("{ document.getElementById(d).style.display=\"block\"; }\n");
    sb.append("else { document.getElementById(d).style.display=\"none\"; }\n}\n");
    sb.append("//--></script>\n");
    out.write(sb.toString());
    out.newLine();
}

// body

    private String linkShowHideContent(String pathname, String divname){
    StringBuilder sb = new StringBuilder();
    sb.append("<a href=\"javascript:ReverseDisplay('");
    sb.append(divname);
    sb.append("')\">");
    sb.append(pathname);
    sb.append("</a>");
    return sb.toString();

}

// content

    out.write(linkShowHideContent("hidden content", "ex"));
    out.write("<div id=\"ex\" style=\"display:block;\">");
    out.write("<p>Content goes here.</p></div>");

Update // a chunk of the html code from the page that reflects the problem

<html><head><style type="text/css">

#table {font-family:"Trebuchet MS", Arial, Helvetica, sans-serif; width:75%;border-collapse:collapse;}
#table td, #table th {font-size:1em;border:1px solid #98bf21;padding:5px 8px 5px 10px;}
#table th {font-size:1.2em;text-align:left;padding-top:5px;padding-bottom:4px;background-color:#A7C942;color:#fff;}
#table tr.alt td {color:#000;background-color:#EAF2D3;}
</style></head><body>
<h2> QUERY RESULTS </h2>
<script type="text/javascript" language="javascript">
<!--function HideContent(d) {
document.getElementById(d).style.display="none";}
function ShowContent(d) {
document.getElementById(d).style.display="block";}
function ReverseDisplay(d) {
if(document.getElementById(d).style.display=="none")
{ document.getElementById(d).style.display="block"; }
else { document.getElementById(d).style.display="none"; }
}
//--></script>
<a href="javascript:ReverseDisplay('ex')">hidden content</a>
<div id="ex" style="display:block;">
<p>Content goes here.</p></div>
+1  A: 

It looks right to me from what I can tell, as long as your IDs are unique in the html code.

You might want to try using firebug (firefox plugin), it's a great javascript debugging tool.

Calvin L
+3  A: 

A few tips:

  • Since the JavaScript block you generate with addShowHideScript() is static, I'd suggest not to write it to every page, but put in a separate file in include it with <script type="text/javascript" src="yourScript.js"></script>.

  • You can drop language="JavaScript". Its obsolete and unnecessary.

  • Don't "comment out" your Javascript with HTML comments (<!-- ... -->). That's also obsolete and unnecessary.

  • It's customary to let the name of JavaScript functions start with a small letter.

RoToRa
thanks for the tips, i have removed the language="" and comments..Just wondering why is it better to move the static function off to a separate file?
posdef
It's more a matter of organization. In your case where you are generating stand alone report With HTML (if I understand you correctly) I guess it would make no big difference. Advantages would, for example, would normally be that, if you need to, you can change the script once in one place and don't need to touch all the scripts in all the HTML pages you have.
RoToRa
+1 for good advice
dpb
+2  A: 

I executed your methods and this resulted (formatting not included :D):

<script type="text/javascript" language="JavaScript">
<!--
function HideContent(d) {
  document.getElementById(d).style.display="none";
}

function ShowContent(d) {
  document.getElementById(d).style.display="block";
}

function ReverseDisplay(d) {
  if(document.getElementById(d).style.display=="none") { 
    document.getElementById(d).style.display="block"; }
  else { 
    document.getElementById(d).style.display="none"; 
  }
}
//-->
</script>

<a href="javascript:ReverseDisplay('ex')">hidden content</a>
<div id="ex" style="display:block;">
  <p>Content goes here.</p>
</div>

It works just fine on IE 8 and Firefox 3.6.

Do you have JavaScript disabled in your browser or some security restrictions that cut your scripts from running?

EDIT: Based on the code you posted, the problem seems to be this:

<!--function HideContent(d) {

If you write it like this it works:

<!--
function HideContent(d) {
dpb
Odd.. I have recently tried with SeaMonkey and Opera as well, still no magic.. They all have JS enabled by the way..Could the formatting have any role in this? I am no expert but if my memory serves me right js shouldnt be too picky about the syntax (formatting that is)
posdef
@posdef: see the edit to my answer. It worked initially because I formatted the thing. Without the formatting it can't find the ReverseDisplay function.
dpb
thanks! i guess it did the trick :)
posdef
You shouldn't "comment out" JavaScript with HTML comments (<!-- ... -->) in the first place. It obsolete and unnecessary.
RoToRa
@RoToRa: I agree with that, but to a certain point. Those were used to hide JS code from old browsers that do not support JS and that just rendered the script content as text in the page. For legacy apps that you have to maintain, it can be useful to know. Also, because we "youngsters" don't use them we don't get bitten by not inserting a line break after the <!-- that opens the comment. Even if you don't insert them it's good to know what they are and what they do even if to ignore them. We learn every day (sometimes the hard way): http://docstore.mik.ua/orelly/web/jscript/ch18_02.html
dpb
It is very reasonable to assume that there are **no more** browsers out there that do not recognize the `script` element and do not at least ignore it. And if there were they would have much bigger problems with modern websites than displaying some script.
RoToRa
BTW, that web page you are referring to, is so out of date, it is not even funny. It's 13 years old and came out before IE 5 (possibly even IE 4). AFAIK that chapter doesn't even exist any more in the current edition.
RoToRa
OK. OK. I read you!
dpb