Created
July 11, 2023 03:23
-
-
Save herdianf/90c9500fb0684a6fa1ad3d5b84b2dc4b to your computer and use it in GitHub Desktop.
innerHTML and execute script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setInnerHTML(elm, html) { | |
elm.innerHTML = html; | |
elm.querySelectorAll("script").forEach(function(oldScriptEl) { | |
let newScriptEl = document.createElement("script"); | |
[].slice.call(oldScriptEl.attributes).forEach(function(attr) { | |
newScriptEl.setAttribute(attr.name, attr.value); | |
}); | |
newScriptEl.appendChild(document.createTextNode(oldScriptEl.innerHTML)); | |
oldScriptEl.parentNode.replaceChild(newScriptEl, oldScriptEl); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment