I'm wondering if it's possible to execute some javascript inside an ld+json script. For example "window.location.hostname"
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "WebSite",
"url": "http://" + window.location.hostname
}
</script>
JSON-LD stands for JavaScript Object Notation for Linked Data, which consists of multi-dimensional arrays (think: list of attribute-value pairs). It is an implementation format for structuring data analogous to Microdata and RDFa.
JSON Schema is primarily used for describing large sets of self-contained data, used as a data descriptor it's similar to XML schema. JSON LD is a way to describe data use of the web, more similar to schema.org or meta descriptors. It's consumed for more than one type of object and can be used to discover schema.
RDFa is commonly used in both the head and body sections of the HTML page. Google recommends using JSON-LD for structured data whenever possible.
Developers not concerned with Linked Data only need to understand JSON, and know to include but ignore the @context property, to use the basic functionality in JSON-LD. Compatibility. A JSON-LD document is always a valid JSON document.
No, scripts of the type "application/ld+json" won't be executed. But, you could do something like this:
<script>
var el = document.createElement('script');
el.type = 'application/ld+json';
el.text = JSON.stringify({
"@context": "http://schema.org",
"@type": "WebSite",
"url": "http://" + window.location.hostname
});
document.querySelector('body').appendChild(el);
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With