8

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

<script type="application/ld+json">
{
   "@context": "http://schema.org",
   "@type": "WebSite",
   "url": "http://" + window.location.hostname
}
</script>
4

1 回答 1

19

不,“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>
于 2014-12-18T23:13:01.977 回答