Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Benjamin McCarthy
Benjamin McCarthy
2,002 Points

JSON Schema for SEO - using getElementById to pull from HTML - valid?

To try and work out if it's possible to write some JS to put in the header of a template, that then uses IDs to create a schema for each page (to help with SEO). I have done the following:

<head>
<script>
var urlVar = window.location.href;
var datePublishedVar = document.getElementById(datepublishedid);
var headineVar = document.getElementById(headlineid);
var imageMainVar = document.getElementById(mainimageid);
var articleBodyVar = document.getElementById(articlebodyid);
{
 "@context" : "http://schema.org", 
"@type" : "Article",
"url" : urlVar,
"datePublished" : datePublishedVar,
    "headline": headineVar,
    "inLanguage": "en_gb",
  "image" : imageMainVar,
  "articleBody" : articleBodyVar,
    "publisher": {
        "@context": "https://schema.org",
        "@type": "Organisation",
        "url": "http://www.YOURWEBSITE.com",
        "name": "YOUR COMPANY NAME",
        "description": "YOUR COMPANY DESCRIPTION"
    },

}
</script>


<head>
<body>
<p id="datepublishedid">02/05/2018</p>
<h1 id="headlineid">This is The Headline</h1>
<p id="articlebodyid">The Body of Text</p>
<img id="mainimageid" src="https://www.google.co.uk/images/branding/googlelogo/1x/googlelogo_color_272x92dp.png">
</head>

<footer>
</footer>

Would this work or am i off the mark? It feels like i'm getting this wrong

1 Answer

Steven Parker
Steven Parker
229,788 Points

Most likely, since it is not part of a statement or expression, the object literal would be mistaken by the JavaScript engine as the start of a code block and the contents would cause a syntax error.

Is there some code missing that would do something with this object?

Benjamin McCarthy
Benjamin McCarthy
2,002 Points

Thank you for your reply Steven

a JSON-LD structured data snippet would usually be:

<script type="application/ld+json">
{
  "@context": "http://schema.org",
  "@type": "Organization",
  "url": "http://www.example.com",
  "name": "Unlimited Ball Bearings Corp.",
  "contactPoint": {
    "@type": "ContactPoint",
    "telephone": "+1-401-555-1212",
    "contactType": "Customer service"
  }
}
</script>

is <script type="application/ld+json"> important for it to be rendered as an object? (readable by Google bot)

Am i being thick that you can't add variables to this kind of script?

Steven Parker
Steven Parker
229,788 Points

To avoid confusing the browser, be sure to separate the JSON block from the JavaScript code, and give each the appropriate type of script tags.

Also, it looks like the closing head tag is missing a slash, but there's an extra closing head tag inside the body.

My comments are just related to how the code would affect a browser. I'm not familiar with the operation of Google bot or what it might do with this data.