(function( openx, $, undefined ) {
- var count = 0;
- var slots = {};
- var ads = new Array();
+ var
+ id,
+ node,
- openx.fetch_ads = function(server, zones) {
+ count = 0,
+ slots = {},
+ queue = [],
+ ads = [],
+ output = [];
- var spc;
- spc = "<script type='text/javascript' src='";
- spc += location.protocol == 'https:' ? 'https://' + server + ':8443':'http://' + server;
- spc += "/www/delivery/spc.php?zones=";
+ openx.show_ads = function(server, zones) {
+
+ var
+ domain = document.location.protocol == 'https:' ? 'https://' + server + ':8443':'http://' + server,
+ name,
+ src = domain;
+
+ /**
+ * 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=";
/** Only fetch banners, that are really included in this page */
- $('.oa').each(function() {
- var node = $(this);
- for(var name in zones) {
+ for(name in zones) {
+ $('.oa').each(function() {
+ var
+ node = $(this),
+ id;
if (node.hasClass(name)) {
- var id = 'oa_' + ++count;
+ id = 'oa_' + ++count;
slots[id] = node;
- spc += escape(id + '=' + zones[name] + "|");
+ queue.push(id);
+ src += escape(id + '=' + zones[name] + "|");
}
- }
- });
+ });
+ }
- spc += "&nz=1&source=" + escape(OA_source);
- spc += "&r=" + Math.floor(Math.random()*99999999);
- spc += "&block=1&charset=UTF-8";
+ src += "&nz=1&source=" + escape(OA_source);
+ src += "&r=" + Math.floor(Math.random()*99999999);
+ src += "&block=1&charset=UTF-8";
- if (window.location) spc += "&loc=" + escape(window.location);
- if (document.referrer) spc += "&referer=" + escape(document.referrer);
+ if (window.location) src += "&loc=" + escape(window.location);
+ if (document.referrer) src += "&referer=" + escape(document.referrer);
- spc+="'></script>";
+ $.getScript(src, init_ads);
- document.write(spc);
- document.write("<script type='text/javascript' src='http://" + server + "/www/delivery/fl.js'></script>");
- }
+ src = domain + '/www/delivery/fl.js';
+ $.getScript(src);
+ }
- openx.render_ads = function() {
+ function init_ads() {
- /** Render the fetched ad-banners... */
- for (var id in slots) {
- // alert(id + ": " + OA_output[id]);
- if (typeof(OA_output[id]) != 'undefined' && OA_output[id] != '') {
- document.write("<div id='" + id + "'>");
- document.write(OA_output[id]);
- document.write("</div>");
+ 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);
- // alert('Banner ' + id + ': ' + OA_output[id]);
+ }
+
+ document.write = document_write;
+ document.writeln = document_write;
+
+ render_ads();
+
+ }
+
+ function render_ads() {
+
+ while (ads.length > 0) {
+
+ var result, src, inline, i;
+
+ id = ads.shift();
+ node = slots[id];
+
+ node.slideDown();
+
+ // node.append(id + ": " + node.attr('class'));
+
+ /**
+ * If output was added via document.write(), this output must be
+ * rendered before other banner-code from the OpenX-server is rendered!
+ */
+ insert_output();
+
+ 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] + ' ' // << 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*['"]?([^'"]*)['"]?\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);
+ insert_output(); // << The executed inline-code might have called document.write()!
+ }
+ else {
+ /** script-tag with src-URL! */
+ 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.append(OA_output[id]);
+ OA_output[id] = "";
}
+ /** All entries from OA_output were rendered */
+
+ id = undefined;
+ node = undefined;
}
- openx.show_ads = function() {
+ /** This function is used to overwrite document.write and document.writeln */
+ function document_write() {
- /** Show the rendered banners */
- for (var i=0; i<ads.length; i++) {
- var ad = $('#'+ads[i]).detach();
- slots[ads[i]].append(ad);
+ if (id == undefined)
+ return;
+
+ for (var i=0; i<arguments.length; i++)
+ output.push(arguments[i]);
+
+ if (id != ads[0])
+ /**
+ * Re-Add the last banner-code to the working-queue, because included
+ * scripts had added markup via document.write(), which is not
+ * proccessed yet.
+ * Otherwise the added markup would be falsely rendered together with
+ * the markup from the following banner-code.
+ */
+ ads.unshift(id);
+
+ }
+
+ /**
+ * 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!