简体   繁体   English

如果使用JavaScript动态构建标记,Schema.org标记是否有效?

[英]Does Schema.org markup work if markup is dynamically built with JavaScript?

I have a page where some events are dynamically loaded by reading some JSON with JavaScript. 我有一个页面,通过使用JavaScript读取一些JSON动态加载一些事件。 I build a div for every event with the Event Schema.org markup. 我使用Event Schema.org标记为每个事件构建一个div

Google's testing tool doesn't read this markup. Google的测试工具不会读取此标记。 Is it because of an error in the markup, or is it because of the dynamic loading? 是因为标记中的错误,还是因为动态加载?

The HTML code of one Event is: 一个Event的HTML代码是:

<div class="evento well" itemscope itemtype="http://schema.org/Event">
   <meta itemprop="startDate" content="2015-03-20T20:00:00.000Z">
   <meta itemprop="endDate" content="2015-01-21T20:00:00.000Z">
   <div class="dataEvento">
      <div class="dayWeekEvento">venerdì</div>
      <div class="dayNumEvento">20</div>
      <div class="monthEvento">Marzo</div>
   </div>
   <div class="datiEvento">
      <div class="oraEvento">ore 21:00</div>
      <div class="titoloEvento"><span itemprop="name">Titolo evento</span></div>
      <div class="luogoEvento" itemprop="address" itemscope="" itemtype="http://schema.org/PostalAddress"><a href="https://www.google.it/maps/place/Milano" target="_blank"><span class=" glyphicon glyphicon-map-marker" aria-hidden="true"></span> <span itemprop="addressLocality">Milano</span></a></div>
   </div>
</div>

Google's documentation only mentions that they can consume dynamically added structured data if the syntax JSON-LD is used: Google的文档仅提及如果使用语法JSON-LD,他们可以使用动态添加的结构化数据:

Also, Google can read JSON-LD data even when it is dynamically injected into the page's contents, such as by Javascript code or embedded "widgets". 此外,Google可以读取JSON-LD数据,即使它被动态注入到页面内容中,例如通过Javascript代码或嵌入式“小部件”。

This does not necessarily mean that they can't read it in case of other syntaxes (like Microdata or RDFa), but at least they don't document it. 这并不一定意味着在其他语法(如Microdata或RDFa)的情况下它们无法读取它,但至少它们不会记录它。

That their testing tool doesn't read it may or may not mean something (it could be that the tool doesn't handle this but their internal system does). 他们的测试工具没有读取它可能会或可能不会意味着什么( 可能是该工具不处理这个,但他们的内部系统确实如此)。 However, there should be no issue with your actual markup, as you can easily test it yourself by pasting your markup instead of entering your URL. 但是,实际标记应该没有问题,因为您可以通过粘贴标记而不是输入URL来轻松地自行测试。

If you're having trouble validating schema markup with the Google testing tool, you can create the json-ld snippet with JS, which also allows you to manipulate the data if needed like: 如果您在使用Google测试工具验证模式标记时遇到问题,可以使用JS创建json-ld片段,这也允许您在需要时操作数据,如:

<script> 
    (function(){
       var data = {
            "@context": "http://www.schema.org",
            ...
        }
        var script = document.createElement('script');
        script.type = "application/ld+json";
        script.innerHTML = JSON.stringify(data);
        document.getElementsByTagName('head')[0].appendChild(script); 
    })(document);
</script>

It depends on what type of markup it is. 这取决于它是什么类型的标记。 From Google : 来自Google

JSON-LD is supported for all Knowledge Graph features, sitelink search boxes, Event Rich Snippets, and Recipe Rich Snippets; 所有Knowledge Graph功能,附加链接搜索框,Event Rich Snippets和Recipe Rich Snippets都支持JSON-LD; Google recommends the use of JSON-LD for those features. Google建议将JSON-LD用于这些功能。 For the remaining Rich Snippets types and breadcrumbs, Google recommends the use of microdata or RDFa. 对于剩余的Rich Snippets类型和面包屑,Google建议使用微数据或RDFa。

It should work but I know people have reported problems with the testing tool. 它应该工作,但我知道人们已经报告了测试工具的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM