PDA

View Full Version : Javascript repeats when not supposed to


Garath531
02-17-2008, 04:52 AM
I have this code (this is the relevant part).

if(location.href.match(/showtopic=/gi)) {
var tDiv = document.getElementsByTagName('div')
for(k=0;k<tDiv.length;k++) {
if(tDiv[k].className == "signature" && tDiv[k].innerHTML.match(/\[items\](.*)\[\/items\]/gi)) {
var string = unhash("1Z18i13n15l18i");
document.write(string);
}
}
}

When I execute it it writes string an infinite amount of times. Eventually Firefox prompts me to stop the script. Here is the function unhash.

function unhash(str) {
newStr = "";
strIs = str.replace(/\d+/gi, "");
strIs = strIs.split("")
for(k=0;k<strIs.length;k++) {
if(strIs[k] == 'A') {newStr += 'Z';}if(strIs[k] == 'B') {newStr += 'Y';}if(strIs[k] == 'C') {newStr += 'X';}if(strIs[k] == 'D') {newStr += 'W';}if(strIs[k] == 'E') {newStr += 'V';}if(strIs[k] == 'F') {newStr += 'U';}if(strIs[k] == 'G') {newStr += 'T';}if(strIs[k] == 'H') {newStr += 'S';}if(strIs[k] == 'I') {newStr += 'R';}if(strIs[k] == 'J') {newStr += 'Q';}if(strIs[k] == 'K') {newStr += 'P';}if(strIs[k] == 'L') {newStr += 'O';}if(strIs[k] == 'M'){newStr += 'N';}if(strIs[k] == 'N') {newStr += 'M';}if(strIs[k] == 'O') {newStr += 'L';}if(strIs[k] == 'P') {newStr += 'K';}if(strIs[k] == 'Q') {newStr += 'J';}if(strIs[k] == 'R') {newStr += 'I';}if(strIs[k] == 'S') {newStr += 'H';}if(strIs[k] == 'T') {newStr += 'G';}if(strIs[k] == 'U') {newStr += 'F';}if(strIs[k] == 'V') {newStr += 'E';}if(strIs[k] == 'W') {newStr += 'D';}if(strIs[k] == 'X') {newStr += 'C';}if(strIs[k] == 'Y') {newStr += 'B';}if(strIs[k] == 'Z') {newStr += 'A';}if(strIs[k] == 'a') {newStr += 'z';}if(strIs[k] == 'b') {newStr += 'y';}if(strIs[k] == 'c') {newStr += 'x';}if(strIs[k] == 'd') {newStr += 'w';}if(strIs[k] == 'e') {newStr += 'v';}if(strIs[k] == 'f') {newStr += 'u';}if(strIs[k] == 'g') {newStr += 't';}if(strIs[k] == 'h') {newStr += 's';}if(strIs[k] == 'i') {newStr += 'r';}if(strIs[k] == 'j') {newStr += 'q';}if(strIs[k] == 'k') {newStr += 'p';}if(strIs[k] == 'l') {newStr += 'o';}if(strIs[k] == 'm'){newStr += 'n';}if(strIs[k] == 'n') {newStr += 'm';}if(strIs[k] == 'o') {newStr += 'l';}if(strIs[k] == 'p') {newStr += 'k';}if(strIs[k] == 'q') {newStr += 'j';}if(strIs[k] == 'r') {newStr += 'i';}if(strIs[k] == 's') {newStr += 'h';}if(strIs[k] == 't') {newStr += 'g';}if(strIs[k] == 'u') {newStr += 'f';}if(strIs[k] == 'v') {newStr += 'e';}if(strIs[k] == 'w') {newStr += 'd';}if(strIs[k] == 'x') {newStr += 'c';}if(strIs[k] == 'y') {newStr += 'b';}if(strIs[k] == 'z') {newStr += 'a';}
}
return newStr;
}


Does anyone see the problem with it?

Ultragames
02-17-2008, 06:13 AM
I'm not too sure about your endless loop, but this should be a MUCH clearner version of your unhash function:

Untested:
function unhash(str) {
newStr = "";
strIs = str.replace(/\d+/gi, "");
for(k=0; k<strIs.length; k++) {
if( strIs.charCodeAt(k)>=97 && strIs.charCodeAt(k)<=122 ){
newStr += 97+(26-(strIs.charCodeAt(k)-97)).toString(16);
} else if( strIs.charCodeAt(k)>=65 && strIs.charCodeAt(k)<=90 ){
newStr += 65+(26-(strIs.charCodeAt(k)-65).toString(16));
} else {
newStr += strIs.charAt(k);
}
}
return newStr;
}

Garath531
02-17-2008, 03:58 PM
No, my function unhashes, but thanks anyway.

Ultragames
02-17-2008, 09:15 PM
. . . Okay. I don't think you understand how the function works then.

All your original unhash function does it swap a letter for its opposite in the alphabet, in a case sensitive manner. Therefore, your hash function will both hash, and unhash, depending on what string you pass it. As will my version.

Therefore, you should still be able to use my much cleaner version of the function. Give it try, they will have the same results.