Javascript inside LD JSON

前端 未结 1 591
无人共我
无人共我 2021-01-02 11:02

I\'m wondering if it\'s possible to execute some javascript inside an ld+json script. For example \"window.location.hostname\"



        
相关标签:
1条回答
  • 2021-01-02 11:28

    No, scripts of the type "application/ld+json" won't be executed. But, you could do something like this:

    <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>
    
    0 讨论(0)
提交回复
热议问题