Will google index content declared in an inline javascript variable?
Asked Answered
Z

2

7

I can't seem to find a clear answer on this. If my page looks like this:

<html>
<head>
  <script>
    var mypets = [ 
        { 'type': 'dog', 'name':'rover' }, 
        { 'type': 'cat', 'name':'kitty' }];
    $('#btnShowEm').click(function() { $('#emptyAtFirst').text(mypets[0].name); });
 </script>
</head>
<body>
  <div id='emptyAtFirst'></div>
  <button id='btnShowEm">ShowEm</button>
</body>
<html>

Will Google index the 'mypets' data even though it is not displayed in the html at the start? The real world page declares lots of content in a similar javascript object and displays bits of it in response to user actions; I want to know if I have to do something more in order to get the page to index based on all the content, not just what is visible in html before the javascript stuff runs.

Zeringue answered 14/10, 2013 at 18:52 Comment(6)
Yes, you should provide a plain HTML version of your page to be rated better. The bot reads and indexes all of your page, but how those data will show up in any search results is a different story (and Google's business secret)Corollary
Take a look at #5365478 and #826775Wickiup
@StevenV very similar links, but neither of those appear to be exact duplicates of this. As far as I can understand, the OP is asking whether the actual JavaScript is indexed, rather than HTML that is manipulated with JavaScript. I could be mistaken though, the wording a bit vague.Articular
That is correct; I wanted to know if the javascript would be indexed. The actual page would have thousands of divs if I were to expose them all, the JS exposes just a few at a time so as not to bog down the browser. On the other hand, gbot takes the hit for the huge doc so my plan now is to have the server expose all the elements in HTML if googlebot user-agent is detected.Zeringue
Although I applaud you for being very elegant and clever from a technical standpoint, I would advise against this strategy. This falls in the category of SEO cloaking, and it is very risky, because if google detects that you are serving up different content to the googlebot search spider than to your site visitors, you can get banned or severely penalized in the SERP's. I would not do this if I were you. See tip #10 here: stonetemple.com/articles/top-10-bad-SEO-ideas.shtmlWeaponry
Good point. The content is actually the same for both search engine and user, it's just that the user gets to pick the bits that are made visible from moment to moment. But the bot might not get that, and it won't care about the logic of it. Not sure I have a choice though; can't ruin the user experience for the sake of a search bot. If google doesn't want to index it well, c'est la vie.Zeringue
W
2

I do not recommend that you rely on content stored in JavaScript to drive your SEO efforts.

Google has said "If fancy features such as JavaScript, cookies, session IDs, frames, DHTML, or Flash keep you from seeing all of your site in a text browser, then search engine spiders may have trouble crawling your site." That's why Google recommends using Lynx to preview your site to see how their spider sees it.

Although it is almost certain that Google does read all your JS, I doubt they give you as much (if any) rank juice from JavaScript code. It is better to place your important content in your HTML. Perhaps HTML5 data-attributes are more favorable to search engines. If this is the case, to maximize the likelihood that the search engine will read it, you will probably want to have all this content in the HTML when the initial page loads, rather than populating it via JavaScript after page load.

Further reading here.

Weaponry answered 14/10, 2013 at 20:10 Comment(0)
S
1

No. Google does not index <script> and <style> content, not even nested tags.

Test pages — HTML and XHTML, .

Straightjacket answered 15/10, 2020 at 17:9 Comment(1)
This shows that Google might not be using them to show search results. Anyway, they might be following links inside <script> tags in their crawling process. This might be relevant to dynamic websites that do SSR/SSG and maintain "dehydrated" states as plain JS objects in inline scripts. e.g. <script>var state = { path: "/hello.html" }</script>Tribble

© 2022 - 2025 — McMap. All rights reserved.