2014-11-27 12 views
5

Mi chiedo se sia possibile eseguire alcuni javascript all'interno di uno script ld + json. Ad esempio, "window.location.hostname"Javascript dentro LD JSON

<script type="application/ld+json"> 
{ 
    "@context": "http://schema.org", 
    "@type": "WebSite", 
    "url": "http://" + window.location.hostname 
} 
</script> 

risposta

9

No, gli script di tipo "application/ld + json" non viene eseguita. Ma potresti fare qualcosa del genere:

<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

caro @diongley hai salvato il mio tempo, –