This week's book giveaway is in the Design forum.
We're giving away four copies of Building Microservices and have Sam Newman on-line!
See this thread for details.
The moose likes HTML, CSS and JavaScript and the fly likes bizarre Javascript problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Building Microservices this week in the Design forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "bizarre Javascript problem" Watch "bizarre Javascript problem" New topic

bizarre Javascript problem

Bob Grossman
Ranch Hand

Joined: Dec 18, 2008
Posts: 69
A snippet of Javascript code:

Now here's an alert that appears:

I cannot for the life of me figure out why the various molstruct.indexOf() are returning values of -1 when molstruct clearly contains these strings.

I'm using Safari 5.0.3, MacOS 10.5.8. I don't know whether the same problem occurs in other browsers/systems.

Here's another tidbit: molstruct.replace(/\n/g, ' ').replace(/\r/g, ' ') returns an unchanged value for molstruct. It's almost as if the Javascript methods are operating on an empty string, not molstruct.

Any ideas?
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15385
Can you edit that string in that alert, it is long and not wrapping in the code block.

Are you sure molstruct is a string?

Bob Grossman
Ranch Hand

Joined: Dec 18, 2008
Posts: 69
Ah, you hit the nail on the head. I wasn't aware of the typeof operator. Safari thinks the variable is an object, not a string. Now I just have to figure out why Safari doesn't realize it's a string. Thanks for your help.
Have you checked out Aspose?
subject: bizarre Javascript problem
It's not a secret anymore!