I'm encoding all email addresses on a website as ROT-13, then decoding the addresses using Javascript (to avoid spam). However, the decoding just flat-out doesn't work in IE 7 or 8. Works splendidly in Chrome, Safari, Firefox. Any ideas on what is going wrong?
UPDATE The link "href" is being properly decoded, and the links actually work properly when clicked. So only the link text (HTML content) is failing at being decoded.
Here is the code I'm using:
/***********************************************
DECODE ROT13 EMAIL LINKS
***********************************************/
$('a.email-encoded').each(function() {
$(this).attr('href', rot13x($(this).attr('href')));
$(this).html(rot13x($(this).html()));
});
function rot13x(s) {
var rxi = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
var rxo = 'NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm5678901234';
var map = [];
var buf = '';
for (z = 0; z < rxi.length; z++) {map[rxi.substr(z, 1)] = rxo.substr(z, 1);}
for (z = 0; z < s.length; z++) {
var c = s.charAt(z);
buf += (c in map ? map[c] : c);
}
return buf;
}
Turns out the problem has nothing to do with the ROT-13 decoding.
There is a "bug" in Internet Explorer regarding the 'href' attribute of email links. If you update the 'href' using javascript, IE automatically updates the text of the link to match the 'href'.
So in my code, first the 'href' was correctly decoded, then IE set the link text to match the new, decoded 'href'. Then, we decoded the already-decoded link text, inadvertently encoding it again.
The solution was to store the decoded link text as a var first, and then use that for the link text after the 'href' was done decoding. Like so:
$('a.email-encoded').each(function() {
var oldHref = $(this).attr('href');
var newHref = rot13x(oldHref);
var newLink = rot13x($(this).html());
$(this).attr('href', newHref);
$(this).html(newLink);
});