Skip to content

Instantly share code, notes, and snippets.

@herdianf
Created July 11, 2023 03:23
Show Gist options
  • Save herdianf/90c9500fb0684a6fa1ad3d5b84b2dc4b to your computer and use it in GitHub Desktop.
Save herdianf/90c9500fb0684a6fa1ad3d5b84b2dc4b to your computer and use it in GitHub Desktop.
innerHTML and execute script
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