Ad

JSON-LD Created By Javascript Is Not Displayed In View-source

- 1 answer

There are 'duplicates' to my question but nobody answer my question :(

I am trying to generate JSON-LD structured data with javascript. Everything was fine until I checked from the tool, the JSON-LD code generated by javascript doesn't show up on the test table. When I proceed to view source on chrome it do not display the JSON-LD code.

You can see it live here

The code is below

<script id='myJSONID' type='application/ld+json'></script>
<script type='text/javascript'>
//<![CDATA[
$('#myJSONID').text(function() {
    return JSON.stringify({
        "@context":"https://schema.org",
        "@type":"Review",
        "author": {
            "@type":"Person",
            "name":"Nhà đất Sơn Nghĩa",
            "sameAs":"https://www.nhadatsonnghia.com/"
        },
        "inLanguage":"vi",
        "itemReviewed": {
            "@type":"Product",
            "name": "" + document.title,
            "description":"" + document.getElementsByName("description")[0].content,
            "aggregateRating": {
                "@type": "AggregateRating",
                "ratingValue": "5",
                "bestRating": "5",
                "ratingCount": "20"
             }
        }
    });
});
//]]>
</script>

Does the above code have any problem? If not then how the JSON-LD code generated by javascript can be displayed when view-source (Ctrl + U)

Any suggestions are appreciated!

Thanks so much!

Ad

Answer

Because it shows only raw data and not exute Javascript in view-source, Try to use server side language to achieve this.

Ad
source: stackoverflow.com
Ad