Hiding JavaScript

In general, search engines tend to weight the text that appears at the top of your pages more heavily than that which comes further down. Think of it like reading a newspaper article. The first paragraph of the article tells you all the main points. Similarly, search engines may analyze your opening text to try and understand what your page is about.

Consequently, using JavaScript at the top of your documents may impact your relevancy. Some search engines may index your code first, then your HTML copy next. That means they may put the biggest priority on scripting code, rather than your nice opening paragraphs that contain the important terms you hope to be found for.

JavaScript Indexed

Here’s an example from April 2000, when I first wrote this article. At that time, I searched for “document.write” at AltaVista, which is a fairly typical piece of JavaScript code. My goal was to find any pages that AltaVista may have indexed that contained this text. The number three site was for the Black Entertainment Network, with a listing like this:

n’); } if ( ShockMode ) { document.write(”); document.write(‘ ‘); document.write(‘ ‘);
document.write(‘ ‘); document.write(”); document.write(”);…

As you can see, AltaVista clearly picked up some of the JavaScript from this page. Obviously, using a meta description tag might have solved the bad listing for the page, but that wouldn’t have kept the JavaScript from still being indexed and possibly degrading relevancy.

One thing that should prevent JavaScript from being indexed is to surround the code with a comment tag. In fact, that’s standard practice to keep non-JavaScript capable browsers from seeing JavaScript. Nevertheless, the code can still get through. One reason may be the use of a > symbol in the JavaScript itself. For instance, one piece of JavaScript on the BET home page example above started out with an opening comment tag, like this:

= 3)

Finally, there comes a closing comment tag:


What is supposed to happen is that nothing between would be indexed by a search engine that ignores comments. But remember that code string that contains the > character? Using an old browser to simulate a search engine, I found that this was interpreted to be the closing comment tag. Thus, everything from that point on was treated as HTML text.

Another Look

How about another example? OK, here’s one from June 2003, this time in a search for “boxster unlimited” at Google. The page in position three from Porsche Austria was listed like this:

Boxster Unlimited – Porsche Austria Gesellschaft mbH & Co.
… 30′,’ Informationsmaterial’”, [‘javascript:openkdm();’, Christophorus’”,
[‘pages/boxster_unlimited.php?H_ID=4&HS_ID=54’,’Boxster Unlimited’” ”; / Sub URLs …

The text in the description all came from a long JavaScript segment at the top of the page. Google selected this text for the description, as opposed to text from the body copy, because the first reference to “boxster unlimited” appeared in the JavaScript text.

The use of a meta description tag MAY have helped solve this problem. One of the JavaScript solutions below, especially the external .js file option, almost certainly would have fixed the issue.

JavaScript Solutions

If you can’t depend on the comment tags to hide your code, what else can you do? I recommend moving JavaScript to the bottom of your page, whenever possible. That will at least help ensure that it won’t be the first text encountered.

An even better solution is to make use of .js files. These are “external” files that contain your JavaScript code. You refer to them from within your page, and then the JavaScript code is only loaded by browsers that understand JavaScript. Since most search engines don’t read JavaScript, they should never import the information. More resources about them are below.

Finally, I’ve also been asked about using the NOSCRIPT tag as a means to hide JavaScript code. This won’t work. That tag is only meant for displaying text to browsers that don’t read JavaScript. For instance, say you had important information on your page that was only viewable to those with JavaScript browsers. NOSCRIPT lets you send an error message to those using non-JavaScript browsers, while browsers that can read JavaScript will ignore the text within the NOSCRIPT tags. Since search engines are generally like old, non-JavaScript browsers, the text you put in a NOSCRIPT tag is actually text you are explicitly trying to make them see.

Using .js files when creating HTML pages

Basic information about using .js files, from Netscape.

Loading External JS Files for DHTML Browsers
WebReference, Nov. 2, 1999

Long, comprehensive tutorial on the use of .js files, especially helpful in making sure you are HTML 4 compliant.

Footer Text

This code from The JavaScript Source shows is an actual example how .js files can be used.

HTML 4.01 Specification: Scripts

Discusses the NOSCRIPT tag and the issue of hiding script with comment tags. It also suggests a solution to the closing comment tag bug that I described above.

Related reading

Simple Share Buttons