Obscure an EMail Address on a Web Page

Tired of the spam? If you have a web page on the net with your email address on it, and virtually anybody has a published link to it, chances are that you're getting spam due to a spammer's automated crawler finding the "mailto:" or <blah>@<blah>.<blah> on your page. I wrote a quick - and admittedly somewhat lame - JavaScript to obscure email addresses in a web page. But getting free lame code beats taking the time to develop it yourself, right?

I place the following in the header of my web page (the <head> and </head> are to show where the code goes in the HTML page). OK, actually, I put this in a server-side include that I load in the header. Same difference. But if you know about that, then I can leave the details as an exercise for the reader. And if you don't know about server side includes, it may be worth the research, if they are supported by your web host. In any case, the header code is:

 

      
         <head>

            <script type="text/javascript">

               <!--

                  // Obscures a text email address from spam-bots

                  function Obscure( account, domain, suffix )

                  {

                     // Build the addr and return the text

                     string = account + "@" + domain + "." + suffix;

                     return( string );

                  }



                  // Obscures a mail link from spam-bots ( subject parameter is optional)

                  function ObscureLink( account, domain, suffix, linkText, subject )

                  {

                     // Be a little sneaky about making a link - this is the lame

                     // part - but it can't hurt...

                     string = "<";

                     string += "a ";

                     string += "hr";

                     string += "ef";

                     string += "=\"";

                     string += "ma";

                     string += "il";

                     string += "to:"

         

                     // Add the address

                     string += Obscure( account, domain, suffix );

         

                     // Do a subject if one was provided

                     if (null != subject)

                     {

                        string += "?sub";

                        string += "ject=";

                        string += subject;

                     }

                     // close the link start

                     string += "\">";

         

                     // Add the link text

                     string += linkText;

         

                     // And finish the link

                     string += "";

         

                     return( string );

                  }

               -->

            </script>

         </head>

      

Then, if I want to show an address on the web page, I do this:

 

      
         My email address is

         <script type="text/javascript">

            <!--

               document.write( Obscure( "Jim", "Campanell", "com" ) );

            -->

         </script>

      

which displays as:

My email address is

Or, if I want a mailto: link to my address:


         My email address is

         <script type="text/javascript">

            <!--

               document.write( ObscureLink( "Jim", "Campanell", "com", Obscure( "Jim", "Campanell", "com" ) ) );

            -->

         </script>

      

which displays as:

My email address is

An extra parameter can be added for the ObscureLink call to specify a subject for the mail window that gets generated:


         My email address is

         <script type="text/javascript">

            <!--

               document.write( ObscureLink( "Jim", "Campanell", "com", "here", "Obscure EMail comment" ) );

            -->

         </script>

      

click on the link to see the subject line inserted:

My email address is


This page is maintained by Jim Campanell. Please send comments to