convertForJavaScript

User 870ab5b546

03-09-2006 19:58:06

Hi,





We use the following lines in a JSP page:





Code:
String lastAnswer = ChemUtils.toAppletParam(userresult[k-1].lastResponse) + "";     


 s = "Points: <span style=\"font-weight:bold; color:" + statColor + ";\">"


                        + numPts + "</span><br>" + "(" + numAtts


                        + " <a href=\"javascript:showMol('" + lastAnswer


                        + "')\" title=\"View most recent answer\">"


                        + attsStr + "</a>)";








This code generates HTML that looks like this:





Code:
            Points: <span style="font-weight:bold; color:#000000;">1.0</span><br>(1 <a href="javascript:showMol('<?xml version=\"1.0\" ?>\n<MDocument>\n  <MChemicalStruct>\n    <molecule molID=\"m1\">\n      <atomArray\n          atomID=\"a1 a2 a3 a4 a5 a6\"\n          elementType=\"C Br N N C Br\"\n          formalCharge=\"0 0 0 1 0 -1\"\n          x2=\"-4.550000190734863 -3.0100001907348632 -8.341666221618652 2.799999952316284 4.339999952316284 7.699999809265137\"\n          y2=\"0.23333333432674408 0.23333333432674408 0.2916666567325592 0.2916666567325592 0.2916666567325592 0.3499999940395355\"\n          />\n      <bondArray>\n        <bond atomRefs2=\"a1 a2\" order=\"1\" />\n        <bond atomRefs2=\"a4 a5\" order=\"1\" />\n      </bondArray>\n    </molecule>\n  </MChemicalStruct>\n  <MRectangle id=\"o2\" color=\"#000000\">\n    <MPoint x=\"-9.391666412353516\" y=\"1.5166666507720947\" />\n    <MPoint x=\"-2.0416667461395264\" y=\"1.5166666507720947\" />\n    <MPoint x=\"-2.0416667461395264\" y=\"-1.1666666269302368\" />\n    <MPoint x=\"-9.391666412353516\" y=\"-1.1666666269302368\" />\n  </MRectangle>\n  <MRectangle id=\"o3\" color=\"#000000\">\n    <MPoint x=\"1.0500000715255737\" y=\"1.5166666358709335\" />\n    <MPoint x=\"8.399999737739563\" y=\"1.5166666358709335\" />\n    <MPoint x=\"8.399999737739563\" y=\"-1.166666641831398\" />\n    <MPoint x=\"1.0500000715255737\" y=\"-1.166666641831398\" />\n  </MRectangle>\n  <MPolyline id=\"o4\" color=\"#000000\" headLength=\"0.8\" headWidth=\"0.5\">\n    <MRectanglePoint pos=\"5\" rectRef=\"o2\" />\n    <MRectanglePoint pos=\"7\" rectRef=\"o3\" />\n  </MPolyline>\n  <MEFlow id=\"o5\" color=\"#000000\" arcAngle=\"150.0\" headSkip=\"0.15\"\n          headLength=\"0.5\" headWidth=\"0.4\" tailSkip=\"0.25\">\n    <MAtomSetPoint atomRefs=\"m1.a3\" />\n    <MAtomSetPoint atomRefs=\"m1.a3 m1.a1\" weights=\"0.25 0.75\" />\n  </MEFlow>\n  <MEFlow id=\"o6\" color=\"#000000\" arcAngle=\"-254.995522631729\"\n          headSkip=\"0.25\" headLength=\"0.5\" headWidth=\"0.4\" tailSkip=\"0.15\">\n    <MAtomSetPoint atomRefs=\"m1.a1 m1.a2\" />\n    <MAtomSetPoint atomRefs=\"m1.a2\" />\n  </MEFlow>\n</MDocument>\n')" title="View most recent answer">attempt</a>)








Which in turn ends up looking like this:





Points: 1.0


(1 \n\n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n \n\n')" title="View most recent answer">attempt)





Rather than





Points: 1.0


(1 attempt)





The code works fine when userresult[k-1].lastResponse is in MOL format.





Any ideas?





-- Bob

ChemAxon 7c2d26e5cf

04-09-2006 15:14:15

When you would like to print mrv source to the html page, you should encode brackets ( '<', '>'). If you miss it, the browser try to interpret mrv tags like html tags.


Use &lt; for '<' and &gt; for '>' to display them on HTML page.


When you give the mrv source like applet parameter, don't use the above encoding.

User 870ab5b546

04-09-2006 16:41:40

So you're saying, even in a JavaScript parameter, the < and > tags need to be encoded as &lt; and &gt; ? How odd. OK, we can add a couple of lines. But your convertForJavaScript() method should take care of it for us, shouldn't it?

ChemAxon 9c0afc9aaf

05-09-2006 07:04:45

Bob, Tamas,





There is no reason to HTML encode '<' and '>'.





The real problem is quotation:





You start the html href with ["], then use ['] for the JavaScript, and inside the MRV file there comes an other ["].


The latter means the end of the href, since the the browser is not (and cannot be) aware, that this is part of a JavaScript where we intended to escape the double quotation.


In other words there is no JavaScript interpretation inside a href, it is treated as a simple string.





The solution: put the source into a JavaScript variable, so it's treated properly according to JavaScript syntax.





An example for the generated page:











Code:






<html>


<head>





<script LANGUAGE="JavaScript1.1">


<!--





answer= '<?xml version=\"1.0\" ?>\n<MDocument>\n  <MChemicalStruct>\n    <molecule molID=\"m1\">\n      <atomArray\n          atomID=\"a1 a2 a3 a4 a5 a6\"\n          elementType=\"C Br N N C Br\"\n          formalCharge=\"0 0 0 1 0 -1\"\n          x2=\"-4.550000190734863 -3.0100001907348632 -8.341666221618652 2.799999952316284 4.339999952316284 7.699999809265137\"\n          y2=\"0.23333333432674408 0.23333333432674408 0.2916666567325592 0.2916666567325592 0.2916666567325592 0.3499999940395355\"\n          />\n      <bondArray>\n        <bond atomRefs2=\"a1 a2\" order=\"1\" />\n        <bond atomRefs2=\"a4 a5\" order=\"1\" />\n      </bondArray>\n    </molecule>\n  </MChemicalStruct>\n  <MRectangle id=\"o2\" color=\"#000000\">\n    <MPoint x=\"-9.391666412353516\" y=\"1.5166666507720947\" />\n    <MPoint x=\"-2.0416667461395264\" y=\"1.5166666507720947\" />\n    <MPoint x=\"-2.0416667461395264\" y=\"-1.1666666269302368\" />\n    <MPoint x=\"-9.391666412353516\" y=\"-1.1666666269302368\" />\n  </MRectangle>\n  <MRectangle id=\"o3\" color=\"#000000\">\n    <MPoint x=\"1.0500000715255737\" y=\"1.5166666358709335\" />\n    <MPoint x=\"8.399999737739563\" y=\"1.5166666358709335\" />\n    <MPoint x=\"8.399999737739563\" y=\"-1.166666641831398\" />\n    <MPoint x=\"1.0500000715255737\" y=\"-1.166666641831398\" />\n  </MRectangle>\n  <MPolyline id=\"o4\" color=\"#000000\" headLength=\"0.8\" headWidth=\"0.5\">\n    <MRectanglePoint pos=\"5\" rectRef=\"o2\" />\n    <MRectanglePoint pos=\"7\" rectRef=\"o3\" />\n  </MPolyline>\n  <MEFlow id=\"o5\" color=\"#000000\" arcAngle=\"150.0\" headSkip=\"0.15\"\n          headLength=\"0.5\" headWidth=\"0.4\" tailSkip=\"0.25\">\n    <MAtomSetPoint atomRefs=\"m1.a3\" />\n    <MAtomSetPoint atomRefs=\"m1.a3 m1.a1\" weights=\"0.25 0.75\" />\n  </MEFlow>\n  <MEFlow id=\"o6\" color=\"#000000\" arcAngle=\"-254.995522631729\"\n          headSkip=\"0.25\" headLength=\"0.5\" headWidth=\"0.4\" tailSkip=\"0.15\">\n    <MAtomSetPoint atomRefs=\"m1.a1 m1.a2\" />\n    <MAtomSetPoint atomRefs=\"m1.a2\" />\n  </MEFlow>\n</MDocument>\n';








function showMol() {


    alert(answer);   


}





-->


</script>


</head>





<body>





Points: <span style="font-weight:bold; color:#000000;">1.0</span><br>(1


<a href="javascript:showMol()" title="View most recent answer">attempt</a>)





</body>


</html>











Some clarification for others who may read this:





Your ChemUtils.toAppletParam calls our HTMLTools.convertForJavaScript(), which is the correct way to convert the structure source before using it as a JavaScript string.





http://www.chemaxon.com/jchem/doc/api/chemaxon/util/HTMLTools.html#convertForJavaScript(java.lang.String)





Best regards,





Szilard

User 870ab5b546

05-09-2006 12:27:36

Unfortunately, I can't use this solution, because the snippet I sent you is embedded in a for loop, so I would need to have a separate Javascript variable for every instance of the loop.





But maybe I can devise some other solution. At least, I can replace the " characters with &quot;.





-- Bob

ChemAxon 9c0afc9aaf

05-09-2006 14:15:08

Quote:
Unfortunately, I can't use this solution, because the snippet I sent you is embedded in a for loop, so I would need to have a separate Javascript variable for every instance of the loop.
You could also use two loops . One declares an array of structures, the other one prints the links in the body.





See the attached html for an example output.
Quote:
At least, I can replace the " characters with &quot;.
This may also work.





Best regards,





Szilard