0

I'm wanting to have a very limited amount of structured data (ideally microdata) added using javascript, especially using the document's lastModified property. It seems rather bizarre and counter-intuative to have to update the structured data for each small edit on a webpage.

According to this and google's documentation the best way to do this is to use JSON-LD rather than microdata. I've looked at the JSON-LD documentation and examples but as far as I am unable to find an example of this. How do I do this?

The page currently has the javascript that sets the contents (innerHTML) of <span id="moddate"></span> to the lastModified date.

This is the JSON-LD I have so far, how do I made modifiedDate work via javascript?

<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "WebPage",
"author": "http://plus.google.com/12345678",
"breadcrumb": "Articles > MedicalArticle > Psychology",
"datePublished": "2014-05-01",
"dateModified": "2015-05-01",  
}
</script>

The is already existing microdata for the WebPage schema including author and additional fields.

  • 1
    Is your question how to modify the value of a JSON-LD property with JavaScript? If yes, this probably belongs to Stack Overflow. – unor Jul 17 '15 at 14:06
0

this involved a code snippet and needs jquery plus moment.js to set the citation_online_data meta tag to the current date.

Add to the <head> tag

<script type="text/javascript"> 
$(document).ready(function(){
var dateModified = moment(document.lastModified).format("YYYY/MM/DD"); 
$('head').append( '<meta name="citation_online_date" content="' +  dateModified + '">');

//create JSON-LD for lastModified and citation_online_date
var el = document.createElement('script');  
el.type = 'application/ld+json';  
el.id = 'jsonld';
el.text = JSON.stringify({ "@context": "http://schema.org", "@type": "WebPage", "url": "http:"+window.location.href, "datePublished": "+dateModified+" }); 
$('body').append(el);
});
</script>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.