Matt Cutts says Google can access some content within JavaScript and AJAX. There is evidence Google crawls the web through Chrome which could explain how Google could execute JavaScript and AJAX.
But the question keeps coming up: can Google execute JavaScript and AJAX? Can they really? This question has been asked quite a few times in SEOmoz’s Q&A forum as well as other webmaster forums. Personally, I have several clients (many of whom are online marketers and web professionals) ask me this question on a semi-regular basis.
So, what is the answer? Can Google really read the content you place inside JavaScript? The typical response (and what I recommend) is that while Google can index some content inside JavaScript, the really important content should be placed in normal HTML so that you know it can be indexed. While that is fantastic advice, that doesn’t seem to satisfy some people who ask this question.
The only way to know what Google can really do with JavaScript is to test out different types of JavaScript and AJAX then see what Google indexes. The problem is you can do many things with AJAX and JavaScript; your imagination is the limit. Instead of testing everything you can do, I tested a few basic scripts websites use when interacting with content. After assembling the sample code containing test content, it was a matter of waiting until Google indexed the content to see what Google was able to index. All results were pulled on June 12, 2012.
In JavaScript, there are a few basic ways of putting content into the body of a web page. One of those is the document.write method. This method writes a string of text directly to the web page document.
When the document.write method was used directly on the page Google crawled, Google was able to index the content. This is one of the simplest, if not the simplest, methods in JavaScript so really this should come as no surprise.
However, in many cases, JavaScript (and the content that you place inside the JavaScript) is included on a web page via an external JavaScript file. The second test file that had the same document.write script with the content inside but placed the code and content in an external JavaScript file that was then linked to the page Google was crawling.
Google did index that test page but Google did not index the content located in the external file. This, and subsequent tests, seems to suggest Google cannot read the content contained in an external JavaScript file. This does make sense, as the content is not part of the file Google crawled.
Another way of writing content in JavaScript is to use innerHTML. This property updates the content of an existing element on the web page. For example, if you have a paragraph on your website you can change that paragraph to have new content using innerHTML.
Like with document.write, there were two test files. The first placed the JavaScript containing the content on the page Google was crawling.
The second test file placed the JavaScript and content in an external JavaScript file.
The results were the same as document.write. When the script was on the same web page, Google indexed the content with no issues. When the content was in the external JavaScript file, Google did not index the content even though the second test page was indexed.
After testing the basics, I wanted to test something slightly more complex. One common usage of JavaScript is to create tabbed content. Tabs create a great user experience and help break up longer pages. There are many ways to put tabs on your page. I tested two different off-the-shelf scripts: jQuery tabs and AJAX tabs.
The scripts behave the same when viewed in a browser: splitting the content into tabs. However, the jQuery script has all of the content on one page. The code then shows and hides parts of the content based on which tab is clicked. Alternatively, the AJAX tabs script spreads the content over different files even though it looks like the content exists on one file when viewed in the browser.
Because the jQuery tab approach has the content on one page, Google was able to index all of that content. More importantly, Google associated all of that content with the same page. This is critical if you are relying on that content for optimization.
However, with the AJAX tabs Google indexed the external files separately; index of Google indexing one page, Google indexed multiple pages. As a result, the content inside the tabs was not associated with the same page but instead with separate pages. In other words, Google was not able to understand that the AJAX tabs meant all that content should be associated onto a single page.
Yes, Google can execute some JavaScript to find content but Google has limitations on what it can do, and what it can understand. The best practice remains the same: put the content you want Google to crawl and index in basic HTML. For example, use jQuery tabs to put the content on one file instead of AJAX tabs that spreads out the content across several files. In short, make it easy for Google to access your content.