How to hide an email address using Javascript PDF Print E-mail
Written by Joris Bijvoets   
Monday, 21 December 2009 07:15

Placing your e-mail address on a web page makes it vulnerable for web spiders, searching for e-mail addresses to spam. With a simple Javascript you can hide your e-mail address for those spiders while human beings won't notice your trick.

You can use the Javascript-functionString.fromCharCode for this. To make a link to the address This e-mail address is being protected from spambots. You need JavaScript enabled to view it you use the following code:

<a href="javascript:void(location.href='mailto:'+String.fromCharCode(97,64,98,46,99,111,109)+'?')">My Email address</a>

This javascript looks like the following: My Email address.

The serie of numbers you see in the function are the ASCII numbers for each character.

To make my life easier, I wrote a small VB.net program to make the character array without effort. The basic code is written below:

Dim iCnt As Integer
Dim sConverted As String = ""

'On my form I have a Textbox txtEmail, which contains the e-mail address
Dim sEmail As String = txtEmail.Text

sConverted &= "<a href="/ & Chr(34) & "javascript:void(location.href='mailto:'+String.fromCharCode("

For iCnt = 0 To sEmail.Length - 2
sConverted &= Asc(sEmail.Substring(iCnt, 1)) & ","
Next

sConverted &= Asc(sEmail.Substring(sEmail.Length - 1, 1))

'The Textbox txtDisplay contains the text to display.
sConverted &= ")+'?')" & Chr(34) & ">" & txtDisplay.Text & "</a>"

'To make life even more easy, I place my script on the clipboard, so that I can paste the code directly in HTML.
Clipboard.SetText(sConverted)

You can download the executable here; the source code in vb.net is here. And for online convenience: click here to use the convertor online. But be warned, the lay out of the online tool isn't optimized yet. The stand alone executable is nice and free. Below you can see a screendump of the application.

Convert email to javascript

 

Last Updated on Wednesday, 06 January 2010 06:20