Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript inside LD JSON

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>
like image 889
rdiazroman Avatar asked Nov 27 '14 11:11

rdiazroman


People also ask

Is JSON-LD JavaScript?

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.

What is the difference between JSON and JSON-LD?

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.

Does Google prefer JSON-LD?

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.

Is JSON-LD valid JSON?

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.


1 Answers

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>
like image 147
dlongley Avatar answered Oct 08 '22 05:10

dlongley