[jdom-interest] "&" char in Javascript generated by JDOM

alastair.rodgers at phocis.com alastair.rodgers at phocis.com
Mon Dec 2 01:57:50 PST 2002


Hi Federico, 

Can't you just use the old trick of embedding the JavaScript in an HTML
comment? Since HTML and XML comments are denoted by the same delimiters
("<!--" and "-->"), you can easily add the comment with JDOM. 

So for example, you could have something along the lines of: 

String js = "if (condition1 && condition2) alert(\"Conditions met!\");";
Element script = new Element("script");
script.addContent(new Comment(js));

This will result in the following HTML: 

<script>
<!--
if (condition1 && condition2) alert("Conditions met!");
-->
</script>

Since your JavaScript is in an XML comment, the '&' chars won't be escaped,
but HTML browsers will still interpret the JavaScript correctly. 

Hope this helps. 

Al.



> -----Original Message-----
> From: Federico [mailto:fmeschini at tin.it] 
> Sent: 29 November 2002 17:06
> To: jdom-interest at jdom.org
> Subject: [jdom-interest] "&" char in Javascript generated by JDOM
> 
> 
> Hi all,
> 
> I've written a java class for the automatic creation of a 
> number of HTML files. I use the package JDOM for the creation 
> of the HTML elements in the file. I have to insert a 
> javascript function in the head 
> section, so I
> creat a <script> element and attach it into the <head> 
> element. Then I set the text content of the <script> element, 
> with the javascript I need, using the escape char for the 
> linee feeds and the quotes.
> 
> But the javascript contains the "&&" chars and when I create 
> the html files they have all become "&amp;" and so the file 
> call for an error in the <script> element.
> 
> So I should obtain to avoid the transformation from to "&" to "&amp;"
> 
> Asking to an XML newsgroup I've found that if I'm creating an 
> XHTML (XML) 
> file escaping the ampersands is
> correct and any XHTML processor must accept them. But if I'm 
> creating HTML 
> (SGML) , I should need a serializer which knows about the 
> differences between HTML (SGML) and XHTML (XML).
> 
> What kind of class of the JDOM package can I use to obtain 
> such serializer?
> 
> Thank you
> Federico
> 
> 
> 
> 



More information about the jdom-interest mailing list