I'm converting a jQuery script into Javascript but when I try to append via jQuery.append(data) it creates a proper script in head tag of page
jQuery
if (data) {
$('head').append(data);
}
while when I do it via Javascript it only appends the data as a string and not as a script tag
Javascript
if (data) {
appendTo("head", document.createTextNode(data));
}
If I try to append without document.createTextNode by simply doing
if (data) {
document
.getElementsByTagName("head")[0]
.appendChild(data);
}
then it throws following error
Uncaught TypeError: Failed to execute 'appendChild' on 'Node': parameter 1 is not of type 'Node'.
while data in has the following (JSON+LD) content
const data = "\n<script type=\"application/ld+json\">\n{\n \"@context\": \"http://schema.org\",\n \"@type\": \"BlogPosting\",\n \"mainEntityOfPage\": {\n \"@type\": \"WebPage\",\n \"@id\": \"http://localhost:8080/?p=abbc\"\n },\n \"url\": \"http://localhost:8080/?p=abbc\",\n \"headline\": \"How To workout.\",\n \"datePublished\": \"2021-12-17T15:15:00-06:00\",\n \"dateModified\": \"2022-05-31T11:01:15-05:00\",\n \"author\": {\n \"@type\": \"Person\",\n \"name\": \"Laura\",\n \"url\": \"http://localhost:8080/?a=laura\"\n },\n \"wordCount\": \"377\",\n \"description\": \"Main description . \"\n}\n</script>"