...

View Full Version : Email encoding script



bglassman
05-22-2006, 12:53 AM
Hi everyone

I am working on/modifying a dom script to automatically convert e-mail addresses in the text format of username [at] domain [dot] com that are inside a pair of em tags into working mailto links.

the script works by getting all em elements in the document, checking to make sure they aren't empty and also that they contain the string \s\[at]\s in them and then converts them to mailto links

the problem im having is that the script is skipping over every other em tag, so it is only creating mailto links for the 1st, 3rd, 5th, etc. em tags I know that the problem occurs in the final line of the function


ems[i].parentNode.replaceChild( a, ems[i] );

because i've added alerts through the code and i know that its cycling through all of the em tags correctly, but when it gets to that last line for some reason it is only replacing the odd ems.

I appreciate any help anyone can give on this, thanks for reading it. (this is inside an external js file, hence the window.onload function)


window.onload = function() {
createmailtoLinks();
}

function createmailtoLinks() {
var ems = document.getElementsByTagName( 'em' );
for( var i=0; i<ems.length; i++ ){
if( ems[i].firstChild &&
ems[i].firstChild.nodeValue.match( /\s+?\[at]\s+?/g ) ){
var str = ems[i].firstChild.nodeValue;
str = str.replace( /\s+?\[(?:dot|period)]\s+?/g, '.' ); // replaces all .
str = str.replace( /\s+?\[(?:at)]\s+?/g, '@' ); // replaces the @
str = str.replace( /\s+?\[(?:dash|hyphen)]\s+?/g, '-' ); // replaces all -
var a = document.createElement( 'a' );
a.setAttribute( 'href', 'mailto:'+str );
a.appendChild( document.createTextNode( str ) );
ems[i].parentNode.replaceChild( a, ems[i] );
}
}
}

Kravvitz
05-22-2006, 09:30 AM
Why <em>?

I don't see a problem with your code. Could you show us the X/HTML that you are testing that function with?


I appreciate any help anyone can give on this, thanks for reading it. (this is inside an external js file, hence the window.onload function)

Good. Using body's inline onload attribute is a very bad practice.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum