• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

weird failure of javascript indexOf()

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
When my JSP page acquires a certain value for the variable molstruct, this code:

alert("molstruct.indexOf('z3=') = " + molstruct.indexOf('z3=')
+ ", molstruct =\n" + molstruct);

gives the alert below. As you can see, this particular value of molstruct contains the string 'z3=' about 13 lines down, but molstruct.indexOf('z3=') is returning the value -1. !?!?! I'm using Safari 4.0.3 on MacOS 10.5.8. Any explanations?

molstruct.indexOf('z3=') = -1, molstruct =
<?xml version="1.0" ?>
<cml>
<MDocument>
<MChemicalStruct>
<molecule molID="m1">
<atomArray
atomID="a1 a2 a3 a4 a5 a6 a7 a8 a9 a10"
elementType="C C C C C C C O H H"
x3="-15.785 -16.183600000000002 -14.297500000000001 -12.8099 -12.679124923706055 -14.696100000000001 -15.225475076293947 -11.5715 -12.756699542236328 -15.074450534057618"
y3="6.2081 4.7206 6.6067 6.01560036239624 4.431849828338623 5.1192 7.997949942779542 6.923100000000001 8.291875305175783 3.1038001430511475"
z3="0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0"
/>
<bondArray>
<bond atomRefs2="a1 a2" order="1" />
<bond atomRefs2="a1 a3" order="1" />
<bond atomRefs2="a3 a4" order="1" />
<bond atomRefs2="a4 a5" order="1" />
<bond atomRefs2="a5 a6" order="1" />
<bond atomRefs2="a6 a2" order="1" />
<bond atomRefs2="a3 a7" order="1" />
<bond atomRefs2="a7 a6" order="1" />
<bond atomRefs2="a4 a8" order="2" />
<bond atomRefs2="a3 a9" order="1">
<bondStereo>W</bondStereo>
</bond>
<bond atomRefs2="a6 a10" order="1">
<bondStereo>H</bondStereo>
</bond>
</bondArray>
</molecule>
</MChemicalStruct>
</MDocument>
</cml>
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Moved to the HTML/JavaScript forum.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried it on another browser? Works fine for me under FF.
 
Bob Grossman
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

David Newton wrote:Have you tried it on another browser? Works fine for me under FF.



Yes, I find it works fine under Firefox 3 for Mac as well. It appears to be a Safari bug, then.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it a string variable (the XML)? I'm having a hard time believing this is a bug in the indexOf function, although it's possible.
 
Bob Grossman
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it is a string value. The command that acquires the value is,

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

The applet API says,

java.lang.String getMol(java.lang.String fmt) Gets the molecule in a text format.

It contains \n characters, although that shouldn't make a difference. As noted above, it works perfectly well in Firefox.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try with an ever-shrinking string to see when the issue starts. I mean, I've used indexOf() countless times with no issues.
 
Bob Grossman
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Veddy interesting. I made a test page at http://epoch.chem.uky.edu/epoch-plugin/public/indexOfTest.jsp. Click on the pentagon, click in the applet window, choose Edit -> Clean -> 3D -> Clean in 3D, and press "View indexOf". The javascript call is:



In Safari, the first alert incorrectly gives -1 for both indexOf() statements, whereas the second alert, after the string is forced to be a string, shows positive values for both indexOf() statements. In Firefox, both alerts correctly give positive values for the indexOf() statements. So apparently Safari is not recognizing the applet value as a String, whereas Firefox is so recognizing it. Any ideas why? We have always treated this value as a String; this problem appears to be fairly new.
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No clue--but I'm always suspicious when things look and act like one object but are something else. You could always try typeof foo and see what it actually is.
 
Bob Grossman
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You nailed it. I added a typeof command to the alert. Safari tells me that the variable retrieved from the applet is an object. Firefox 3.0 for Mac tells me that it is a string. Internet Explorer 7.0 for PC also recognizes it as a String. Interestingly, Firefox 2.0 for PC recognizes it as an object, but is still able to find the substrings as if it were a string.

So now, why is Safari not recognizing the object as a string, or converting it into a string when confronted with the indexOf() command, as Firefox 2.0 for PC appears to do?
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Other objects have indexOf methods as well; there's no reason to assume a conversion to string is necessary.

IMO the solution is as you've done--get the string representation of whatever it is and move forward from there.
 
reply
    Bookmark Topic Watch Topic
  • New Topic