File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes weird failure of javascript indexOf() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "weird failure of javascript indexOf()" Watch "weird failure of javascript indexOf()" New topic
Author

weird failure of javascript indexOf()

Bob Grossman
Ranch Hand

Joined: Dec 18, 2008
Posts: 69
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>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61314
    
  66

Moved to the HTML/JavaScript forum.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Have you tried it on another browser? Works fine for me under FF.
Bob Grossman
Ranch Hand

Joined: Dec 18, 2008
Posts: 69
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Dec 18, 2008
Posts: 69
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Dec 18, 2008
Posts: 69
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

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

Joined: Dec 18, 2008
Posts: 69
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
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: weird failure of javascript indexOf()