javascript problem with string from applet

User 870ab5b546

02-03-2011 03:11:58

I'm having a bizarre problem with Javascript methods acting on the MRV string acquired from MarvinSketch.


Here's a snippet of code:


var molstruct = document.responseApplet.getMol('mrv');  
if (molstruct.indexOf('mrvAlias') >= 0) {
toAlert('You probably meant to add a shortcut group such as Ph or Et to your structure, but you added an atom alias instead. (Aliases and shortcut groups look alike, but ACE cannot interpret aliases properly.) To replace the alias with a shortcut group, type the name of the shortcut group in the Marvin window (not in the textbox in the More window), then click on the alias.');
return;
} // if response contains an alias
alert('molstruct.indexOf(\'z3=\') = ' + molstruct.indexOf('z3=')
+ ', molstruct.indexOf(\'<bondStereo>W\') = '
+ molstruct.indexOf('<bondStereo>W')
+ ', molstruct.indexOf(\'<bondStereo>H\') = '
+ molstruct.indexOf('<bondStereo>H')
+ ', molstruct.indexOf(\'<?xml\') = '
+ molstruct.indexOf('<?xml')
+ ', molstruct =\n' + molstruct
);

 


Here's the alert that appears:


molstruct.indexOf('z3=') = -1, molstruct.indexOf('<bondStereo>W') = -1, molstruct.indexOf('<bondStereo>H') = -1, molstruct.indexOf('<?xml') = -1, molstruct =  
<?xml version="1.0" ?>
<cml>
<MDocument>
<MChemicalStruct>
<molecule molID="m1">
<atomArray
atomID="a1 a2 a3 a4 a5 a6 a7"
elementType="C H C H C C N"
isotope="0 2 0 0 0 0 0"
mrvSpecIsotopeSymbolPreferred="0 1 0 0 0 0 0"
x3="-3.698240000000001 -6.182217282306649 -5.238240000000001 -5.238240000000001 -6.008240000000001 -7.548240000000002 -2.1820789781346113"
y3="2.982933333333334 2.4379499478168993 2.982933333333334 1.8929333333333338 4.316666666666667 4.316666666666667 2.9393442988856555"
z3="0.0 0.0 0.0 0.0 0.0 0.0 0.0"
/>
<bondArray>
<bond atomRefs2="a5 a3" order="1" />
<bond atomRefs2="a3 a1" order="1" />
<bond atomRefs2="a3 a2" order="1">
<bondStereo>H</bondStereo>
</bond>
<bond atomRefs2="a3 a4" order="1">
<bondStereo>W</bondStereo>
</bond>
<bond atomRefs2="a5 a6" order="1" />
<bond atomRefs2="a1 a7" order="3" />
</bondArray>
</molecule>
</MChemicalStruct>
</MDocument>
</cml>

Any idea why the Javascript indexOf() method should fail to find the strings that are so obviously present in molstruct?  The replace() method also fails to find \n and \r characters.


Interestingly, if I put molstruct into an array as the only member, then join all members of the array to get a new string, the new string appears to be identical to the original one, but the indexOf() method gives the expected values.


This behavior is occurring in Safari 5 on Mac.  I have no idea whether it happens in other systems.

User 870ab5b546

02-03-2011 20:35:05

This code:


var molstruct = document.responseApplet.getMol('mrv');
alert('typeof molstruct = ' + (typeof molstruct));

gives this alert in Safari:


typeof molstruct = object

So the problem is that Safari does not recognize that the object coming back from MarvinSketch is a string.  Firefox for Linux does recognize that it is a string.  So either there's a bug in Safari, or MarvinSketch is not communicating properly with Safari, or both.


Can you see if MarvinSketch is not typing the variable in such a way that Safari will recognize it as a string? 

ChemAxon 5433b8e56b

07-03-2011 14:18:27

Hi Bob,


the return value of the getMol(String)  is definitelly java.lang.String.


I think this should be enough for javascript to notify that the value is a String.


And regarding to this site it is rather a safari issue.


Regards,
Istvan

User 870ab5b546

07-03-2011 19:44:51

It may be a Safari bug, but if you could fix it, it would not be the first time that you included a workaround for a browser deficiency.


The workaround is simple, though:


var molstruct = String(document.responseApplet.getMol('mrv'));

ChemAxon 5433b8e56b

14-03-2011 17:05:22

Hi Bob,


sorry for the late reply. I missed your post in this topic somehow.


Exactly how do you think to apply this workaround? In the applet code we are returning a String, in the javascript code the client code calls the applet, so i can not imagine a point anywhere in our codebase where i can modify the returning type of the method. Am i missing or misunderstand something?


Regards,
Istvan

User 870ab5b546

14-03-2011 17:09:11

The workaround would not be in your code; it's in the Javascript page that calls the applet.  When Safari retrieves the value from the applet, it's not recognizing the String as a String, so the programmer needs to cast it to a String.  The cast is redundant but inconsequential in other browsers.

ChemAxon 5433b8e56b

14-03-2011 18:42:28

Ah, i see and for now i understand the the whole meaning of your previous post. I have a crazy idea about a possible solution, but currently it is just an instinct.


If we could solve this, then i will let you know. Until that time, the workaround you suggested is the only way to deal with this issue.


Regards,
Istvan

ChemAxon 5433b8e56b

21-06-2011 13:40:10

Hi Bob,


i have checked my theory in practice, it is not working, so it seems we can not implement any workaround for this.


Regards,
Istvan