This example contains a workaround for missing JavaScript to Java communication in Macintosh browsers without LiveConnect support.
Press the Import button to import the molfile into MarvinSketch. Press the Export as Molfile or Export as SMILES button to export the molecule into the specified format. If the browser does not support LiveConnect, pressing the Export as Molfile button displays the molecule string in gzip format encoded with Base64.
If the browser does not support LiveConnect a workaround should be needed for
importing / exporting molecules. See the else if(!isJs2Java)
branch in the code of the
importMol
and exportMol
functions.
<script LANGUAGE="JavaScript"> <!-- //--> </script> <form NAME=MolForm onSubmit="return false"> <textarea NAME="MolTxt" ROWS=20 COLS=70> 3,7-Dihydro-1,3,7-trimethyl-1H-purine-2,6-dione Marvin 07099920012D 14 15 0 0 0 0 0 0 0 0999 V2000 -2.0245 -2.6287 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 -2.0245 -1.0107 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 -1.7156 -0.0596 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 -1.4367 -1.8197 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 -3.8415 -0.8197 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 -5.5735 -0.8197 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 -4.7075 -2.3196 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 -3.8415 -3.8197 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 -2.9755 -2.3196 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 -2.9755 -1.3197 0.0000 C 0 0 0 0 0 0 0 0 0 0 0 0 -3.8415 0.1803 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0 -5.5735 -2.8197 0.0000 O 0 0 0 0 0 0 0 0 0 0 0 0 -4.7075 -1.3197 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 -3.8415 -2.8197 0.0000 N 0 0 0 0 0 0 0 0 0 0 0 0 5 13 1 0 0 0 0 13 7 1 0 0 0 0 7 14 1 0 0 0 0 14 9 1 0 0 0 0 9 10 2 0 0 0 0 5 10 1 0 0 0 0 4 2 1 0 0 0 0 2 10 1 0 0 0 0 9 1 1 0 0 0 0 4 1 2 0 0 0 0 2 3 1 0 0 0 0 5 11 2 0 0 0 0 13 6 1 0 0 0 0 7 12 2 0 0 0 0 14 8 1 0 0 0 0 M END </textarea> <p> <input TYPE=BUTTON VALUE="Import" onClick="importMol('null')"> <input TYPE=BUTTON VALUE="Export as Molfile" onClick="exportMol('mol')"> <input TYPE=BUTTON VALUE="Export as SMILES" onClick="exportMol('smiles')"> </p> </form> <script LANGUAGE="JavaScript1.1" SRC="../../../marvin.js"></script> <script LANGUAGE="JavaScript1.1" SRC="../../../js2java.js"></script> <script LANGUAGE="JavaScript1.1"> <!-- var isJs2Java = isLiveConnect(); // Is JavaScript - Java communication? setPath("../../.."); // the relative path of the resources for the workaround function importMol(opts) { var s = document.MolForm.MolTxt.value; if((document.MSketch != null) && isJs2Java) { document.MSketch.setMol(s, opts); } else if(!isJs2Java) { mparams = "java.lang.String"; if(opts != null) { mparams += ",java.lang.String"; } setMethod("MSketch.setMol",mparams); addMethodParam(s); if(opts != null) { addMethodParam(opts); } runMethod(); } else { alert("Cannot import molecule:\n"+ "no JavaScript to Java communication in your browser.\n"); } } function printMol() { var s = getResult(); s = convertJs2Html(s); document.MolForm.MolTxt.value = s; } function exportMol(format) { if((document.MSketch != null) && isJs2Java) { var s = document.MSketch.getMol(format); s = unix2local(s); // Convert "\n" to local line separator document.MolForm.MolTxt.value = s; } else if(!isJs2Java) { if(format == 'smiles') { setIsCompressed(false); } else { setIsCompressed(true); } setMethod("MSketch.getMol","java.lang.String"); addMethodParam(format); setPostJsMethod("parent.printMol()"); runMethod(); } msketch_name = "MSketch"; msketch_begin("../../..", 540, 480); msketch_end(); //--> </script>
The limitation of this workaround is the size of the result string. The maximum is 2K characters.
Because of the limitied transfer size, the compression of the result data is highly recommended.
Use setIsCompressed
method to specify that the compression of the result is required or not.
If you set it to true
, the the data is sent in compressed (gzip) format with Base 64 encoding.
(true
is the default value.)
Marvin supports both molecule import and export in base64:gzip format.
Since getResult()
returns a string that may contain special characters in an encoded format,
using the convertJs2Html
function is recommended to decode them before display the text in a textbox.