javascript problems when generating html reports from within java

Posted by posdef on Stack Overflow See other posts from Stack Overflow or by posdef
Published on 2010-05-26T08:56:51Z Indexed on 2010/05/26 9:11 UTC
Read the original article Hit count: 337

Filed under:
|
|

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>");

© Stack Overflow or respective owner

Related posts about java

Related posts about JavaScript