我想知道是否可以在 ld+json 脚本中执行一些 javascript。例如“window.location.hostname”
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "WebSite",
"url": "http://" + window.location.hostname
}
</script>
我想知道是否可以在 ld+json 脚本中执行一些 javascript。例如“window.location.hostname”
<script type="application/ld+json">
{
"@context": "http://schema.org",
"@type": "WebSite",
"url": "http://" + window.location.hostname
}
</script>
不,“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>