I’m sorry about the lack of updates these past few days. I’ve been very, very busy wrapping up a few things in my personal life. I should have a significantly greater amount of time next week. I have a few ideas for some other coding related posts to address in the coming weeks. Whenever I get ideas, I write them down. Here is what I have so far:
Month: July 2006
A Brand New Way to Hide Your Email Address
If you haven’t already, I suggest you read the last article I wrote about separating your Javascript from your content. It will give you important background information needed to fully understand what I will be covering today.
Today, I’m going to show you how to embed your email address into any page of your site without fear that some bot will come along and steal it. This method is different from other ones because not only is it “backwards compatible” with older techniques, but it degrades gracefully and is straight forward to non-developers. This feature will work with all modern browsers, is human readable (no garbled text mixed in), and works when you click on it. Most importantly, it doesn’t require you to mark up your beautiful HTML.
Background
Some rogue email spammers use “bots” to automatically go through the internet and find email addresses. Once they find one, they add it to their spam list and you’re email becomes their newest indentured servant — forever. At first, people tried to trick the primitive bots by putting in spaces in their email address, such as in the following example:
some @ email . com
But bots learned to look for the @ symbol and then ignore spaces, so this quickly became obsolete.
So while some clever people think they are safe using address that look like:
some [at] email [dot] com
Or
some[nospam]@emailDOTcom
Or
someREMOVEME at email [period] com
The problem is that bots can be trained to look for these text patterns and get around them. After many programmers spent many hours mucking around with ASCII hashes and other futile measures, it became clear what the real problem was:
Bots can see and read anything a human can see and read!
The other problem is that adding in random words, brackets, and other symbols into your email address makes it very hard to read. This is very much unacceptable for people who are trying to run a business and can’t afford to have less savvy people send emails to salesREMOVE@[DONTSPAM]mybusiness.com.
I’m not going to tell you my idea is future proof. However, it’s been possible for many years now and I’ve never seen it. When and if this catches on, spammers may eventually start learning to get around it. But it won’t be easy. That’s because my method can vary wildly between implementations and can be used along side with any or all of the methods I showed above! More importantly, my method relies on the difference between bots and humans:
Humans click on stuff.
New Idea
The first and most important issue is that when a person clicks on your email link, it should let them email you. This is one of those things that has long since been lost as a readily available feature because so many people are afraid of getting spammed. Sure, maybe you can click on an email link, but you end up with something like the example below (click on it)
I’m sure you find that as annoying as I do. But what if we used Javascript to dynamically change what the href tag did? Well, the thinking-in-the-box idea would be to have this happen on the page load. That presents problems because some bots can run Javascript. The second idea is to change it if and only if someone clicks on the link. Now this makes sense. However, we reach the second problem: how do you store the email address somewhere on the page without the bot being able to see and read it?
How It Works
Primitive implementations used really ugly Javascript that would look something like this:
var myEmail = ‘so’ + ‘me’ + ‘@’ + ’email’;
myEmail += ‘.com’;
And then later on:
<a href=”#” onclick=”return ‘mailto:’ + myEmail”>email me</a>!
You can already see that this is violating the rule of mixing content with code. What happens if your email address changes? Do you really think your designer understands that ugly jumble of a mess? At the very end of the last article, I mentioned the possibility of parsing attributes and putting things together. Using the knowledge you gleamed from there, study the following and listen closely as you hear a lightbulb turn on. The objective is to have an email link to “some@email.com”:
<a href=”#email.com” class=”email-link” id=”some”>email me</a>!
This could have also looked like any of the following:
<a href=”@email.com” class=”email-link” id=”some”>email me</a>!
<a href=”#” class=”email-link” id=”email.com”>some</a>!
<a href=”#” class=”email-link” id=”some”>email.com</a>!
<a href=”#some@” class=”email-link” id=”email.com”>email me</a>!
<a href=”some” class=”email-link” id=”email/com”>email me</a>!
As you can see, there are many, many possibilities. And it is standards compliant and relatively straight forward (especially compared to those other solutions). Best of all, this will work in any browser that supports Javascript. Let’s focus on the first example and go over why I chose that format.
This way, if Javascript is broken, the worst thing that happens is the click does absolutely nothing. Remember, if you aren’t willing to have your email address break when Javascript is turned off then you have *no choice* but to use those HTML based tricks (that bots have already beaten). For 99.99% of the population, this is a pretty worthy sacrifice. However, for those of you who really can’t deal with that, I present you this compromise:
<a href=”#email.com” class=”email-link” id=”some”>some[REMOVE]@NOSPAMemail.com</a>!
This renders into this:
Now if Javascript is disabled, nothing happens but your visitors can still figure out your email address! Although you’d have to pray bots can’t. Anyway, I hope you are starting to see the power of my solution.
Seeing the Address
If we can dynamically re-write the HREF portion of the URL, what’s to stop us from rewriting the innerHTML portion (the stuff between the a tags). Nothing! And this is easily possible. And this should happen during a mouseover so that people can easily read your address if they want.
The Code
Before I give you the demo, please observe the code fragment that summarizes the gist of how this works:
- Look for all tags with the class “email-link”
- For each one create a new onMouseOver that changes the text (innerHTML) into the email address
- For each one create a new onClick that changes the link (href) into an email link
Now I know many of you are looking at my examples and yearning for the actual source code. Please keep in mind that the source code varies greatly depending on how you choose to obfuscate your email address. I’m also confident many of you read this and are now realizing you could dynamically search and replace the “[REMOVE]” and “NOSPAM” text. That said, here is the basic source code, using Prototype‘s getElementsByClassName function:
document.getElementsByClassName(className).each(function(element) {
var emailAddress = this.id + ‘@’ + this.href.split(‘#’).last();
element.onmouseover = function() {
this.innerHTML = emailAddress;
this.href = ‘mailto:’ + emailAddress;
};
})
Now I know some of you will complain about the each() function. As I stressed yesterday, the Prototype library provides shortcuts that make Javascript programming easier. The each method goes through an array, and for each one, passes it into a function as an argument. That function can be defined on the fly, which is what I am doing. Since the array being passed in is all the tags on the page that have “email-link” as its class, the function is then processing each link. The rest, I’m hoping makes a decent amount of sense. The part with emailAddress might be a little confusing, so I will break it down into its simplified equivalent:
var otherPart = this.href; // #mail.com
otherPart = otherPart.split(‘#’); // split it up by the # symbol
otherPart = otherPart.last(); // get the last match found
var emailAddress = this.id; // some
emailAddress = emailAddress + ‘@’; // some@
emailAddress = emailAddress + otherPart; // some@ + mail.com
Demo
If you are burning to see a demo, click here.
Parting Gift
I find that it is becoming difficult to explain my demos without giving you the real source. As such, I have written a Javascript class called CleanCSS. You may download it here.
Designing without Javascript
This article will cover the importance of keeping content, design, and coding elements seperate. I will then explain how you can use advanced techniques to apply Javascript to an element (such as a link) without ever changing the HTML.
Before I begin, I would like to say: I hate CSS. It’s a necessary evil, but I really hate focusing my time and energy on design issues. You see, in an ideal world, designers would design, content providers would provide content, and programmers (that’s me!) would glue it together with backend stuff.
However, this was not how the world spun, my friends. That is, until today.
Obligatory First Post
Hello and welcome to my new blog. While it’s hard to predict exactly how this site will turn out, needless to say, I plan to focus a lot of energy into it for the long term. While I did maintain another web journal, it occured to me that the site no longer accurately represented who I am. In short, I’ve graduated from the kid journal and I’m moving on to writing more techincal, thought-out posts. I am in no denial that this journal will contain humor, bad movie reviews, or small tidbits about my daily life, but the intention is to keep this blog insightful and less about the personal, mundane aspects of my life.
Welcome to My New Blog
Welcome. This is my technical blog. If you are a LAMP web developer with an interest in business this is the blog for you.
Here, you will find 100% original articles related to one of two things: IT news or programming. I try to write as many good programming articles as I can, but as you can imagine, it is very hard coming up with original programming posts day after day. As such, the majority of the time, I cover IT business news. I try to refrain from simply summarizing, and I rarely post an article if I can’t find an interesting observation to make. For you programmers, I write about JavaScript, PHP, and MySQL the most. I try to post at least one article a day on weekdays.
You’ll notice this site uses a couple of JavaScript effects that are entirely unnecessary. While I don’t regularly add effects such as these for commercial sites I do, I thought it would be an interesting challenge. For example, the right two navigation links on top of my home page use “AJAX” (loads content without refreshing the page). But to make things more interesting, I added conditions to the project:
- The AJAX links had to work without javascript
- The content would not be duplicated (no ajax-only versions of a page)
- The site must be XHTML 1.0 compliant
- There must be no Javascript markup inside the content except in the footer and header as include files
- Must integrate with the blog
- Must work in the latest versions of Opera, Firefox, and IE and look nearly if not exactly identical
- No tables for layout placement
Any seasoned web developer knows these requirements are rediculously hard to meet — especially getting AJAX functionality without marking up the links. =) This was a fun challenge. I will eventually document how these obstacles were overcome and update this page. I plan to buy a Mac soon, but until then, I’m afraid I can’t be certain if Mac browsers are supported. I made the tough decision that this site would require javascript. While many commercial entities always have non-javascript versions of their content, I think we’re getting to that point where Javascript is becoming an integral ‘net technology that is pretty much as standard as HTML tags. As such, since this is a personal site, Javascript is “required.” However, as condition #1 said above the site is fully functional without Javascript (although PNG images break in IE).
Article 1:Â Designing without Javascript
Article 2:Â A Brand New Way to Hide Your Email Address