Visible to invisible and vice versa

Content manipulation with JavaScript.

JavaScript is a very powerful technique. It can be used for both purposes: hiding links from users and hiding links from robots. JavaScript code is executed on user side. It means that it is executed by browser. None of search engine robots can fully understand JavaScript. In some cases robots can read JavaScript and parse it like a usual text, however in most cases robots don't event get a clue what JavaScript code is supposed to do.

Hiding links (content) from robots with JavaScript

Since JavaScript is executed in browser, any content that is produced via JavaScript is invisible to robots, but visible to site's users. Here are some popular ways of creating links on the fly:

document.write(''); // you can write any html code you want 
document.createElement('a'); // you can also create a link element via DOM 

In the example above any content can be written dynamically. If you want to learn more about document.write consider visiting w3schools site. And sitepoint's tutorial on how to enhance blockquote html elements should give an insight on how to use document.createElement function

A very similar technique is to add html text to any other html element:

document.getElementById("div").innerHTML = '';
        // you can add any html code inside quotes

For novice link builders it should be enough to be able to spot these functions when viewing site's source code to determine if site owner is doing any tricks.

Another way to create a link dynamically is to simply reassign link target when a link is being created:

document.getElementById("link").href = link;
document.getElementById("link").setAttribute("href",link);

Also it is very popular to do link redirects via a separate function, e.g.:

document.getElementById("link").onclick = redirect_function();

This can also be done by specifying a redirect function in href attribute:

<a href="javascript:redirect_function(link_id)>Click here</a>
<a href="javascript:redirect_function('http://www.someothersite.com')">
Click here</a>

In many cases this is not an elegant solution: some browsers may not understand this type of links (even when JavaScript is turned on), browser's status bar looks bad when user hovers his mouse over such a link; user can accidentally bookmark javascript: link and that would be annoying for him.

Bonus tip: the second line of the last example is double-bad: some search engine robots can still extract urls from href tag.

Hiding links from users

There can be many reasons why some web masters would want to hide parts of sites from their users. One reason is that they want their site to look less spammy. If you build links you should avoid sites hiding them from their users: you would neither want your link to be hidden, nor to get a link from such a shady site).

Hiding can be implemented by removing html element completely:

var el = document.getElementById("link");
el.parentNode.removeChild(el);

or changing element style, e.g.:

document.getElementById("link").style.display = 'none';

There might be many other ways to hide something when manipulating elements style. For example, you can make fonts extremely small or change text color to match background. For more style manipulation tricks see the next chapter "Hiding links with CSS".

It can also be done by replacing existing html code as I've shown before in the hiding links from robots example:

document.getElementById("div").innerHTML = '';
        // you can add any html code inside quotes 

More information about removeChild function can be found at BrainJar Document Object Model tutorial (see section "Working with Text Nodes"). Quirksmode.org has a nice introduction article about CSS objects in JavaScript.

Some general insights in using JavaScript for content manipulation

The whole JavaScript code should be placed inline into a web page. Otherwise when loading JavaScript from an external file or retrieving some content via AJAX a user will notice flicker or similar effect (links/content appearing or disappearing). It is because the browser loads html code first and all the other required documents a bit later.

When content manipulation code is put inline into html, it must be done with care. It is a very common practice to encode links or any other sensitive content that shouldn't be seen by robots. As I wrote before robots can not execute JavaScript, but they can parse it like any other text. Search engines do not like cheating and they'd like to find out if you are cheating in order to ban you. :)

The fact that external JavaScript is not the best solution implies that none of the common JavaScript frameworks/libraries1 can be used, since all of them load from an external file.

Hiding mailto: links with JavaScript

Well, the techniques I described before are meant to fool either a user or search engine bot. If you use that with bad intentions the dupe will get sad. But there is one situation that almost the whole world agrees on. There is nothing bad in fooling spammers or their bots. Guys annoying us with hundreds of messages a day deserve this. Also you'd like to get some protection to make your email cleaner.

Some spammers use bots to gather email addresses from web pages. If you are posting a web page with your email address in it - your mailbox is in danger. So, if you read the previous chapters, you'd guess that you could use JavaScript to display your email without it being seen by spam bots.

The simplest example of this could look like this:

<script type="text/javascript">
var username = "username";
var hostname = "yourdomain.com";
var linktext = "Click Here To Send Me Email";
document.write("<a href=" + "mail" + "to:" + username +
"@" + hostname + ">" + linktext + "</a>")
</script>

This would output your email only to human visitors with real browsers. Using your creativity and imagination you can create more advanced and sophisticated scripts to handle multiple emails and hide them better.

Also there are many great mail encoders online to help you to deal with spammers. A simple but difficult to crack is Hive Enkoder. Another equally powerful one is Metaprog Encoder (remember what I wrote about javascript: links! You can adjust their code a little simply by placing it on onclick rather than href).

This article is a part of SEO design patterns - hiding links. You could also be interested in other articles from this series:

Footnotes

  1. E.g.: jQuery, Prototype, MochiKit and others.