Browse Source

Make document.write working in async mode (!)

Amauri CHAMPEAUX 10 years ago
parent
commit
c11e9afd14
1 changed files with 49 additions and 0 deletions
  1. 49 0
      tarteaucitron.js

+ 49 - 0
tarteaucitron.js

@@ -582,6 +582,55 @@ var tarteaucitron = {
         }
         document.getElementsByTagName('head')[0].appendChild(script);
     },
+    "makeAsync": {
+        "buffer": '',
+        "init": function (url, id) {
+            var savedWrite = document.write,
+                savedWriteln = document.writeln;
+
+            document.write = function (content) {
+                tarteaucitron.makeAsync.buffer += content;
+            };
+            document.writeln = function (content) {
+                tarteaucitron.makeAsync.buffer += content.concat("\n");
+            };
+        
+            setTimeout(function() {
+                document.write = savedWrite;
+                document.writeln = savedWriteln;
+            }, 60000);
+            
+            tarteaucitron.makeAsync.getAndParse(url, id);
+        },
+        "getAndParse": function (url, id) {
+            tarteaucitron.addScript(url, '', function () {
+                document.getElementById(id).innerHTML += tarteaucitron.makeAsync.buffer;
+                tarteaucitron.makeAsync.buffer = '';
+                tarteaucitron.makeAsync.execJS(id);
+            });
+        },
+        "execJS": function forceJsExec(id) {
+            var i,
+                scripts,
+                childId,
+                type;
+
+            scripts = document.getElementById(id).getElementsByTagName('script');
+            for (i = 0; i < scripts.length; i += 1) {
+                type = (scripts[i].getAttribute('type') !== null) ? scripts[i].getAttribute('type') : '';
+                if (type === '') {
+                    type = (scripts[i].getAttribute('language') !== null) ? scripts[i].getAttribute('language') : '';
+                }
+                if (scripts[i].getAttribute('src') !== null) {
+                    childId = id + Math.floor(Math.random() * 99999999999);
+                    document.getElementById(id).innerHTML += '<div id="' + childId + '"></div>';
+                    tarteaucitron.makeAsync.getAndParse(scripts[i].getAttribute('src'), childId);
+                } else if (type.indexOf('javascript') !== -1) {
+                    eval(scripts[i].innerHTML);
+                }
+            }
+        }
+    },
     "fallback": function (matchClass, content) {
         "use strict";
         var elems = document.getElementsByTagName('*'),