Services Products About Us Case Studies Clients
Modern Signal
Modern Signal Home Page
Lighthouse on beach About Us
Offering a full range of development services: strategy, design, application programming, database development
News & Recents Projects
Use Javascript to Link All Email Addresses in a Page
Posted by David Hammond, 2/14/2008

This came up with a site that needed to have a mailto link for email addresses, but other methods of linking the value didn't work. The same basic method could be used to replace any text in a page.

<script type="text/javascript">
//Match email addresses not within tag attributes
var re = /[\w\.\-]+\@([\w\-]+\.)+[\w]{2,4}(?![^<]*>)/ig;

//Function to do replace
function linkEmails(parentNode){
    var nodes = parentNode.childNodes;
    for (var i=0; i < nodes.length; i++){
        //For element nodes other than A nodes, search children
        if (nodes[i].nodeType == 1 && nodes[i].tagName != "A") {
            linkEmails(nodes[i]);

        //For text nodes that match, do replace for parent node.
        } else if (nodes[i].nodeType == 3 && re.test(nodes[i].nodeValue)) {
            parentNode.innerHTML = parentNode.innerHTML.replace(re,"<a href='mailto:$&'>$&</a>")
        }
    }
}

//Set onload event to do replace
window.onload = function(){
    linkEmails(document.body);
}
</script>

I think most of the above code is relatively easy to understand by following the comments I have included, but it could be useful to break down that regular expression to see what it's doing.  The first part matches the email address:
[\w\.\-]+\@([\w\-]+\.)+[\w]{2,4}
The next part uses somewhat more advanced regular expression syntax:
(?![^<]*>)
The (?!...  notation says that the email address will only match is it is not followed by [^<]*>, which should ensure that it is not within a property of an html tag.


June 28, 2010 -- Modern Signal launches redeveloped website for National Health Policy Forum, a nonpartisan research and public policy organization at The George Washington University. The new site includes admin tools to manage email announcements, event invitations and RSVPs, surveys, and an extensive library of publications and meeting materials. The site also features a customized Google Search integration, and a new content management system was integrated within the existing design and information architecture of the site.   -View-
April 22, 2010 -- Modern Signal launches a redesigned website for The NALP Foundation for Law Career Research and Education, a nonprofit organization that works to ensure that the legal community and society at large have a reliable, objective, and affordable source of information.The site includes a content management system; bookstore; and news, events, leadership, and products tools to manage the featured homepage content. -View-
April 5, 2010 --
A redesigned website is launched for Independent Sector, a nonprofit coalition of approximately 600 charities, foundations, and corporate philanthropy programs, collectively representing tens of thousands of charitable groups in every state across the nation. The site includes a content management system, discussion forum integration (phpBB), blog integration (MangoBlog), collaborative authoring wiki, tool to create surveys, video template, among other features.  -View-