I would be glad if someone could take a look at my code and maybe improve it. It's an animation effect to gradually reveal text as if it were being decoded. I wouldn't mind if it could run with jQuery (if it's shorter or better). I uploaded it on jsbin.com. It's all about the effect and not about encryption or decryption.
I've written the following JS Code:
var got,
chars,
decrypted = document.getElementById("decoded"),
encrypted = document.getElementById("encoded");
function change() {
var randstring = "",
rslength = chars.length - got.length,
i;
for (var x=0;x<rslength;x++) {
i = Math.floor(Math.random() * chars.length);
randstring += chars[i];
}
if (randstring[0] === chars[got.length]) {
got += randstring[0];
decrypted.textContent = got;
} else {
encrypted.textContent = randstring;
}
if (chars.length > got.length) {
setTimeout(change, 10);
} else {
encrypted.textContent = "";
}
}
function startdecrypt() {
chars = decrypted.textContent;
decrypted.textContent = "";
got = "";
setTimeout(change, 10);
}
if (window.addEventListener) {
window.addEventListener('load', startdecrypt, false); //W3C
} else {
window.attachEvent('onload', startdecrypt); //IE
}
And this short HTML part:
<span id="decoded">Enjoy this effect!</span>
<span id="encoded"></span>