diff lib/js/urweb.js @ 1702:06791667937e

New JavaScript FFI function: setInnerHTML
author Adam Chlipala <adam@chlipala.net>
date Wed, 14 Mar 2012 10:10:56 -0400
parents ea292bf9431f
children 6f2f74cc4ead
line wrap: on
line diff
--- a/lib/js/urweb.js	Wed Mar 14 09:06:23 2012 -0400
+++ b/lib/js/urweb.js	Wed Mar 14 10:10:56 2012 -0400
@@ -836,6 +836,38 @@
     populate(x);
 }
 
+function setInnerHTML(node, html) {
+    var x;
+
+    if (node.previousSibling && node.previousSibling.closures != undefined) {
+        x = node.previousSibling;
+
+        for (var ls = x.closures; ls; ls = ls.next)
+            freeClosure(ls.data);
+
+        if (node.getElementsByTagName) {
+            var arr = node.getElementsByTagName("script");
+            for (var i = 0; i < arr.length; ++i)
+                killScript(arr[i]);
+        }
+    } else {
+        x = document.createElement("script");
+        x.dead = false;
+        x.sources = null;
+
+        if (node.parentNode)
+            node.parentNode.insertBefore(x, node);
+        else
+            whine("setInnerHTML: node is not already in the DOM tree");
+    }
+
+    var cls = {v : null};
+    var html = flatten(cls, html);
+    x.closures = cls.v;
+    node.innerHTML = html;
+    runScripts(node);
+}
+
 function input(x, s, recreate, type, name) {
     if (name) x.name = name;
     if (type) x.type = type;