LD JSON里面的Javascript

rdi*_*man 5 javascript json json-ld

我想知道是否可以在ld + json脚本中执行一些javascript.例如"window.location.hostname"

<script type="application/ld+json">
{
   "@context": "http://schema.org",
   "@type": "WebSite",
   "url": "http://" + window.location.hostname
}
</script>
Run Code Online (Sandbox Code Playgroud)

dlo*_*ley 14

不,不会执行"application/ld + json"类型的脚本.但是,你可以这样做:

<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>
Run Code Online (Sandbox Code Playgroud)