【发布时间】:2019-04-16 07:07:02
【问题描述】:
我正在使用 dom 元素通过 javscript 设置 google 结构化数据
喜欢这个
function setStructuredDataGeneral() {
structuredData = '{"@context": "https://schema.org/", "@type": "Product", "name": "Ofertas", "image": "<?php echo $wLanding->getFSlideImage(); ?>", "offers": { "@type": "AggregateOffer", "lowPrice": "'+Math.min(...offerPrices)+'", "highPrice": "'+Math.max(...offerPrices)+'", "priceCurrency": "EUR"}}';
var elementScriptStructuredData = document.getElementById("structuredDataGeneral");
var text = document.createTextNode(structuredData);
elementScriptStructuredData.appendChild(text);
}
然后我有一个 sctipt 标记去哪里
<script type="application/ld+json" id="structuredDataGeneral">
</script>
当我检查它工作的网站时,生成预期的结果,问题是当我尝试通过谷歌结构化数据工具 (https://search.google.com/structured-data/testing-tool) 验证 URL 时,标签为空。
是因为这个工具没有加载javascript还是我做错了?
【问题讨论】:
-
这可能与
json/ld应该是从服务器返回的html的一部分,而不是动态添加的事实有关。 (除了goolge没有人跑你页面的js,连google都做的不太好。) -
@felixmosh 所以你的意思是我也通过 javascript 生成标签,然后使用 document.write 或类似的东西将它添加到 html 中?
-
不,如果您使用的是一些服务器端语言,例如 PHP、NodeJs 或类似语言,请在服务器返回的 html 上执行此操作。
标签: javascript dom json-ld google-rich-snippets