LD JSON里面的Javascript [英] Javascript inside LD JSON

查看:67
本文介绍了LD JSON里面的Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>


推荐答案

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

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>

这篇关于LD JSON里面的Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆