Ad-banners are now fetched in the given order
[openx] / openx.js
index eb85805..a01c775 100644 (file)
--- a/openx.js
+++ b/openx.js
@@ -11,6 +11,7 @@
 
   count = 0,
   slots = {},
+  queue = [],
   ads = [];
 
 
 
     var
     domain = document.location.protocol == 'https:' ? 'https://' + server + ':8443':'http://' + server,
+    name,
     src = domain;
 
-    document.write = render;
-    document.writeln = render;
+    document.write = document_write;
+    document.writeln = document_write;
 
     src += "/www/delivery/spc.php?zones=";
 
     /** Only fetch banners, that are really included in this page */
-    $('.oa').each(function() {
-      var
-      node = $(this),
-      name, id;
-      for(name in zones) {
+    for(name in zones) {
+      $('.oa').each(function() {
+        var
+        node = $(this),
+        id;
         if (node.hasClass(name)) {
           id = 'oa_' + ++count;
           slots[id] = node;
+          queue.push(id);
           src += escape(id + '=' + zones[name] + "|");
         }
-      }
-    });
+      });
+    }
 
     src += "&nz=1&source=" + escape(OA_source);
     src += "&r=" + Math.floor(Math.random()*99999999);
 
   function init_ads() {
 
-    for (var id in slots) {
+    var i, id;
+    for (i=0; i<queue.length; i++) {
+      id = queue[i];
       if (typeof(OA_output[id]) != 'undefined' && OA_output[id] != '')
         ads.push(id);
     }
 
-    render_ad();
+    render_ads();
 
   }
 
-  function render_ad() {
+  function render_ads() {
 
-    if (ads.length == 0) {
-      id = undefined;
-      node = undefined;
-      return;
-    }
+    while (ads.length > 0) {
 
-    id = ads.pop();
-    node = slots[id];
+      id = ads.shift();
+      node = slots[id];
 
-    // node.append(id + ": " + node.attr('class'));
+      node.slideDown();
 
-    var result, src, inline;
+      // node.append(id + ": " + node.attr('class'));
 
-    while ((result = /<script/i.exec(OA_output[id])) != null) {
-      node.append(OA_output[id].slice(0,result.index));
-      /** Strip all text before "<script" from OA_output[id] */
-      OA_output[id] = OA_output[id].slice(result.index,OA_output[id].length);
-      result = /^([^>]*)>([\s\S]*?)<\\?\/script>/i.exec(OA_output[id]);
-      if (result == null) {
-        /** Invalid syntax in delivered banner-code: ignoring the rest of this banner-code! */
-        // alert(OA_output[id]);
-        OA_output[id] = "";
-        render_ad();
-        return;
-      }
-      /** Remember iinline-code, if present */
-      src = result[1]
-      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);
-      if (result == null) {
-        /** script-tag with inline-code: execute inline-code! */
-        result = /^\s*<.*$/m.exec(inline);
-        if (result != null) {
-          /** Remove leading HTML-comments, because IE will stumble otherwise */
-          inline = inline.slice(result[0].length,inline.length);
+      var result, src, inline;
+
+      while ((result = /<script/i.exec(OA_output[id])) != null) {
+        node.append(OA_output[id].slice(0,result.index));
+        /** Strip all text before "<script" from OA_output[id] */
+        OA_output[id] = OA_output[id].slice(result.index,OA_output[id].length);
+        result = /^([^>]*)>([\s\S]*?)<\\?\/script>/i.exec(OA_output[id]);
+        if (result == null) {
+          /** Invalid syntax in delivered banner-code: ignoring the rest of this banner-code! */
+          // alert(OA_output[id]);
+          OA_output[id] = "";
+        }
+        else {
+          /** Remember iinline-code, if present */
+          src = result[1]
+          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);
+          if (result == null) {
+            /** script-tag with inline-code: execute inline-code! */
+            result = /^\s*<.*$/m.exec(inline);
+            if (result != null) {
+              /** Remove leading HTML-comments, because IE will stumble otherwise */
+              inline = inline.slice(result[0].length,inline.length);
+            }
+            $.globalEval(inline);
+          }
+          else {
+            /** script-tag with src-URL! */
+            ads.unshift(id); // << The banner might not be rendered fully, or include more calls to document.write().
+            /** 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;
+          }
         }
-        $.globalEval(inline);
-      }
-      else {
-        /** script-tag with src-URL! */
-        ads.push(id); // << The banner might not be rendered fully, or include more calls to document.write().
-        /** Load the script and halt all work until the script is loaded and executed... */
-        $.getScript(result[1], render_ad); // << jQuery.getScript() generates onload-Handler for _all_ browsers ;)
-        return;
       }
+
+      node.append(OA_output[id]);
+      OA_output[id] = "";
     }
-    node.append(OA_output[id]);
-    OA_output[id] = "";
 
-    /** This statement will only reached, when no script-element was rendered! */
-    render_ad();
+    /** All entries from OA_output were rendered */
 
+    id = undefined;
+    node = undefined;
   }
 
-  function render() {
+  function document_write() {
 
     if (id == undefined)
       return;