Fixed for banner-codes, that does not enclose the src-attribute in quotes
[openx] / openx.js
index 3499345..a442f0d 100644 (file)
--- a/openx.js
+++ b/openx.js
     name,
     src = domain;
 
-    document.write = document_write;
-    document.writeln = document_write;
+    /**
+     * Without this option, jQuery appends an timestamp to every URL, that
+     * is fetched via $.getScript(). This can mess up badly written
+     * third-party-ad-scripts, that assume that the called URL's are not
+     * altered.
+     */
+    $.ajaxSetup({ cache: true });
 
     src += "/www/delivery/spc.php?zones=";
 
@@ -66,6 +71,9 @@
         ads.push(id);
     }
 
+    document.write = document_write;
+    document.writeln = document_write;
+
     render_ads();
 
   }
        * If output was added via document.write(), this output must be
        * rendered before other banner-code from the OpenX-server is rendered!
        */
-      if (output.length > 0) {
-        output.push(OA_output[id]);
-        OA_output[id] = "";
-        for (i=0; i<output.length; i++)
-          OA_output[id] += output[i];
-        output = [];
-      }
+      insert_output();
 
       while ((result = /<script/i.exec(OA_output[id])) != null) {
         node.append(OA_output[id].slice(0,result.index));
         }
         else {
           /** Remember iinline-code, if present */
-          src = result[1]
+          src = result[1] + ' ' // << simplifies the following regular expression: the string ends with a space in any case, so that the src-URL cannot be followed by the end of the string emediately!
           inline = result[2];
           /** Strip all text up to and including "</script>" from OA_output[id] */
           OA_output[id] = OA_output[id].slice(result[0].length,OA_output[id].length);
-          result = /src\s*=\s*['"]([^'"]*)['"]/i.exec(src);
+          result = /src\s*=\s*['"]?([^'"]*)['"]?\s/i.exec(src);
           if (result == null) {
             /** script-tag with inline-code: execute inline-code! */
             result = /^\s*<.*$/m.exec(inline);
               inline = inline.slice(result[0].length,inline.length);
             }
             $.globalEval(inline);
+            insert_output(); // << The executed inline-code might have called document.write()!
           }
           else {
             /** script-tag with src-URL! */
-            ads.unshift(id); // << The banner might not be rendered fully, or include more calls to document.write().
+            if (OA_output[id].length > 0)
+              /** The banner-code was not rendered completely yet! */
+              ads.unshift(id);
             /** Load the script and halt all work until the script is loaded and executed... */
             $.getScript(result[1], render_ads); // << jQuery.getScript() generates onload-Handler for _all_ browsers ;)
             return;
     node = undefined;
   }
 
+  /** This function is used to overwrite document.write and document.writeln */
   function document_write() {
 
+    if (id == undefined)
+      return;
+
     for (var i=0; i<arguments.length; i++)
       output.push(arguments[i]);
 
 
   }
 
+  /**
+   * This function prepends the collected output from calls to
+   * document_write() to the current banner-code.
+   */
+  function insert_output() {
+
+    if (output.length > 0) {
+      output.push(OA_output[id]);
+      OA_output[id] = "";
+      for (i=0; i<output.length; i++)
+        OA_output[id] += output[i];
+      output = [];
+    }
+
+  }
+
 } ( window.openx = window.openx || {}, jQuery ));
 
 var OA_output = {}; // << Needed, because IE will complain loudly otherwise!