Az oldal korlátolt funkcionalitással fog rendelkezni, amíg elvégezzük a felhasználói élményt javító karbantartást. Ha egy leírás nem oldja meg a problémáját, és kérdést tenne fel, akkor a támogatási közösségünk a @FirefoxSupport Twitter oldalon tud segíteni, vagy az /r/firefox oldalon a Redditen.

Támogatás keresése

Kerülje el a támogatási csalásokat. Sosem kérjük arra, hogy hívjon fel egy telefonszámot vagy osszon meg személyes információkat. Jelentse a gyanús tevékenységeket a „Visszaélés bejelentése” lehetőséggel.

További tudnivalók

A témacsoportot lezárták és archiválták. Tegyen fel új kérdést, ha segítségre van szüksége.

JavaScript for loop ends prematurely when using String.length, why?

  • 6 válasz
  • 2 embernek van ilyen problémája
  • 12 megtekintés
  • Utolsó üzenet ettől: awasen

more options

Ok, I have this JavaScript code:

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

//current.value contains, e.g. /tmp/myFile.txt

var filename = "";

var tmp = "";

for (var j = 0; j < current.value.length; j++) {

tmp = current.value.substr(current.value.length-j,1);

if (tmp.length > 0) {

if ((tmp.charCodeAt(0) == 92) || (tmp.charCodeAt(0) == 47)) break;

filename = tmp + filename;

}

}

alert(filename);

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I want to get the file name out as "myFile.txt". This works fine in IE8 & 9, Chrome and Opera, but not in FireFox 4. In FF4 I always get "yFile.txt" as the loop ends one position sooner than all the other browsers.

(And, yes, I know this can be done using reg.exp. but due to a PHP bug I can not use the reg.exp. expression using \\ (double back-slashes) so I had to come up with a work-around).

Regards, Anders

Ok, I have this JavaScript code: ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ //current.value contains, e.g. /tmp/myFile.txt var filename = ""; var tmp = ""; for (var j = 0; j < current.value.length; j++) { tmp = current.value.substr(current.value.length-j,1); if (tmp.length > 0) { if ((tmp.charCodeAt(0) == 92) || (tmp.charCodeAt(0) == 47)) break; filename = tmp + filename; } } alert(filename); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ I want to get the file name out as "myFile.txt". This works fine in IE8 & 9, Chrome and Opera, but not in FireFox 4. In FF4 I always get "yFile.txt" as the loop ends one position sooner than all the other browsers. (And, yes, I know this can be done using reg.exp. but due to a PHP bug I can not use the reg.exp. expression using \\ (double back-slashes) so I had to come up with a work-around). Regards, Anders

Módosította: cor-el,

Összes válasz (6)

more options

Try posting at the Web Development / Standards Evangelism forum at MozillaZine. The helpers over there are more knowledgeable about web page development issues with Firefox.
http://forums.mozillazine.org/viewforum.php?f=25
You'll need to register and login to be able to post in that forum.

more options

Thanks for taking time to respond!

I have created a thread there instead. Anyone who wants to follow it can find it here: http://forums.mozillazine.org/viewtopic.php?f=38&t=2207267&e=0

Regards, Anders

Módosította: awasen,

more options

Maybe it is better to start with j=1 because strings start at 0 until length-1 and substr(current.value.length-0) is not defined.

  • current.value.substr(current.value.length-j,1);
more options

Thanks cor-el, you are correct, but unfortunately the issue is that the loop exits one "revolution" too soon. Like in my sample it bails out one character too soon so "abcd" becomes "bcd"...

Regards, Anders

more options

for (var j = 1; j <= current.value.length; j++)

more options
)

Well, the problem is with this line: if ((tmp.charCodeAt(0) == 92) || (tmp.charCodeAt(0) == 47)) break;

If it finds a front- or back-slash it should bail out from the for-loop. The problem is that it bails out one character too soon...

Regards, Anders