【问题标题】:Javascript inside LD JSONLD JSON 中的 Javascript
【发布时间】:2015-01-25 22:59:01
【问题描述】:
我想知道是否可以在 ld+json 脚本中执行一些 javascript。例如“window.location.hostname”
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "WebSite",
"url": "http://" + window.location.hostname
}
</script>
【问题讨论】:
标签:
javascript
json
json-ld
【解决方案1】:
不,“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>