我正在尝试使用JSON-LD实现结构化数据。我所做的是使用jQuery动态获取内容,并将其转换为JSON格式并附加到head元素中。
<script id="dynamicJSONLD"></script>$(document).ready(function(){
var product_name = $(.product-pg .product-name).text();
data = {
"@context": "https://schema.org",
"@type": "Product",
"url": "https://www.example.com/productone",
"name": product_name
};
//creating the script element and storing the JSON-LD
var script = document.createElement('script');
script.type = "application/ld+json";
script.innerHTML = JSON.stringify(data);
document.getElementsByTagName('head')[0].appendChild(script);
//OR
//storing the JSON-LD using ID
$("#dynamicJSONLD").html(JSON.stringify(data));
});这两种方式(创建script元素和存储JSON-LD /使用ID存储JSON-LD )都有效吗?哪种是最好的实现方式?另外,Google crawl是否使用JavaScript动态添加了类似上面的JSON-LD?
发布于 2019-04-05 04:34:36
是的,谷歌可以抓取动态添加的JSON-LD,就像this Google article中关于主题的解释:
JSON-LD数据被动态注入到页面内容中时,例如通过JavaScript代码或内容管理系统中的嵌入式小部件,
Google可以读取JSON-LD数据。
这两种方法肯定都有效,但如果要使用ID存储JSON-LD,则需要向脚本添加所需的type属性:
<script id="dynamicJSONLD" type="application/ld+json"></script>添加完标记后,请确保使用Google's Structured Data Testing Tool进行测试!
发布于 2020-01-19 14:23:14
我使用
var Client = require("node-rest-client").Client;
var client = new Client();
var args = {
headers: { "Accept": "application/ld+json, */*;q=0.5" }
};
client.get(url+query,args, function (data, response) {
......
}https://stackoverflow.com/questions/51376310
复制相似问题