String.substring() or String.indexOf() different in IE/NS?

JavaScript and client side scripting.

Moderator: General Moderators

Post Reply
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

String.substring() or String.indexOf() different in IE/NS?

Post by IceMetalPunk »

I always thought that the substring() function and the indexOf() function worked the same way in each browser (as in, indexOf works the same in IE & NS, and substring() works the same way in IE & NS).

However, I recently needed to get a part of a string using these arguments, parseInt it, and add one to it. The code I came up with worked fine in NS, but ended up checking the wrong part of the string in IE, causing it to return NaN and place it in the wrong spot.

The string was a TD's innerHTML, which was something like

<b>#</b> guests, <b>#</b> members, ....

All the #'s are actual numbers, but they vary from time to time, so they can be 1, 2, 3, or even 4-digit numbers, which is why I'm using a more complicated code than if it were always a fixed number of digits.

Now, here's the code I used:

Code: Select all

temp=tds[p].innerHTML;
temp1=temp.substring(0,temp.indexOf("guests, <b>")+11);
temp=temp.substring(temp.indexOf("guests, <b>")+11,temp.length);
temp2=temp.substring(temp.indexOf("</b>"),temp.length);
temp=temp.substring(0,temp.indexOf("</b>"));
temp=(parseInt(temp)+1).toString();
tds[p].innerHTML=temp1+temp+temp2;
As I said, the tds[p].innerHTML is the <b>#</b> guests, ... HTML.

Now, this works fine in NS, and after rewriting the TD's innerHTML with the new string, it is EXACTLY the same except the number of members is one greater.

However, in IE, the code gets for temp1 <b>#</b> guests,
and all the other substrings() I use return something different than in NS, and end up checking the u in "guests" for the number, of course then returning NaN, so that the end result written to the TD is:

<b>#</b> gNaNuests, ...

The part in BLUE is the part that shouldn't be there, as well as the number of members does not change.

Since the code works in NS, something must be right. But then why does it get the wrong result in IE?

Is there a difference in one of these two functions from NS to IE? If so, can someone pleae tell me what it is so I can fix the code to work cross-browser?

Thanks in advance to anyone who can help (and to those who can't help, but they try anyway).

-IMP ;) :)
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

Wouldn't it be simplier to use regexp? Something along the lines of:

Code: Select all

temp = tds[p].innerHTML;
tds[p].innerHTML = temp.replace(/(<b>\s*)(\d+)(\s*<\/b>\s*guests)/, function(match, submatch1, submatch2, submatch3) {
     return submatch1 + (parseInt(submatch2) + 1).toString() + submatch3;
});
Another approach would be to use DOM: iterate over temp.childNodes until the first non-text node is found (<b> in this case), then modify its content (which is the number of guests).
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post by IceMetalPunk »

Well, I know very little (almost nothing) about regular expressions, which is why I don't use them

I actually am trying to change the number of members, not guests.

The string is something like <b>#</b> guests, <b>#</b> members, ...

I need to check for GUESTS, though, because Members may be in another TD, causing it to use the wrong one, whereas guests will only be in that one TD.

Since (as I said) I know nothing of Regular expressions, could you please modify the code you gave me to change the member number, not the guest number? It would be much appreciated.

Thanks.

-IMP ;) :)
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post by AKA Panama Jack »

Why not use ID tags on each of the numbers and reference your changes by them instead of trying to parse a string.
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post by IceMetalPunk »

I thout I mentioned this, but I guess I forgot.

The page with the HTML I'm parsing is auto-generated by a PHP script, which was not created by me (it's for InvisionFree, if you know what that is).

Therefore, the only HTML I have control over is what I change later with this script, so I can't add anything to the tags as they are, so ID-ing the numbers cannot be done in this case.

-IMP ;) :)
User avatar
IceMetalPunk
Forum Commoner
Posts: 71
Joined: Thu Jul 07, 2005 11:45 am

Post by IceMetalPunk »

At the risk of double-posting and bumping:
IceMetalPunk wrote:Well, I know very little (almost nothing) about regular expressions, which is why I don't use them

I actually am trying to change the number of members, not guests.

The string is something like <b>#</b> guests, <b>#</b> members, ...

I need to check for GUESTS, though, because Members may be in another TD, causing it to use the wrong one, whereas guests will only be in that one TD.

Since (as I said) I know nothing of Regular expressions, could you please modify the code you gave me to change the member number, not the guest number? It would be much appreciated.

Thanks.
-IMP ;) :)
Post Reply