2014-11-27 11 views
5

Ich frage mich, ob es möglich ist, etwas Javascript in einem ld + json Skript auszuführen. Zum Beispiel "window.location.hostname"Javascript in LD JSON

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

Antwort

9

Nein, Skripte vom Typ "application/ld + json" nicht ausgeführt wird. Aber du könntest so etwas tun:

<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

liebe @diongley du hast meine zeit gerettet, –

Verwandte Themen