Activate script tags in raw! DOM output
Scripts inserted via innerHTML (template.content) don't execute. When raw! renders HTML containing <script> tags, recreate them as live elements so the browser fetches and executes them. Fixes editor.js not loading on HTMX navigation to edit pages. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -879,6 +879,16 @@
|
||||
if (typeof val === "string") {
|
||||
var tpl = document.createElement("template");
|
||||
tpl.innerHTML = val;
|
||||
// Scripts in innerHTML don't execute — recreate them as live elements
|
||||
var deadScripts = tpl.content.querySelectorAll("script");
|
||||
for (var si = 0; si < deadScripts.length; si++) {
|
||||
var dead = deadScripts[si];
|
||||
var live = document.createElement("script");
|
||||
for (var ai = 0; ai < dead.attributes.length; ai++)
|
||||
live.setAttribute(dead.attributes[ai].name, dead.attributes[ai].value);
|
||||
live.textContent = dead.textContent;
|
||||
dead.parentNode.replaceChild(live, dead);
|
||||
}
|
||||
frag.appendChild(tpl.content);
|
||||
} else if (val && val.nodeType) {
|
||||
// Already a DOM node (e.g. from children fragment)
|
||||
|
||||
Reference in New Issue
Block a user