JSFiddle - React, Tailwind, and code Playground
JavaScript
var str =
"<scr" + "ipt>console.log('Hello from script')<\/script>" +
"<link>";
// Create an element outside the document to parse the string with
var head = document.createElement("head");
// Parse the string
head.innerHTML = str;
// Copy those nodes to the real `head`, duplicating script elements so
// they get processed
var node, newNode, next;
node = head.firstChild;
while (node) {
next = node.nextSibling;
if (node.tagName === "SCRIPT") {
// Just appending this element wouldn't run it, we have to make a fresh copy
newNode = document.createElement("script");
if (node.src) {
newNode.src = node.src;
}
while (node.firstChild) {
// Note we have to clone these nodes
newNode.appendChild(node.firstChild.cloneNode(true));
node.removeChild(node.firstChild);
}
node = newNode;
}
document.head.appendChild(node);
node = next;
}