1 /** Optimized methods for fetching ad-banners via OpenX */
3 /** see: http://enterprisejquery.com/2010/10/how-good-c-habits-can-encourage-bad-javascript-habits-part-1/ */
5 (function( openx, $, undefined ) {
19 openx.show_ads = function(server, zones) {
22 domain = document.location.protocol == 'https:' ? 'https://' + server + ':8443':'http://' + server,
26 document.write = document_write;
27 document.writeln = document_write;
29 src += "/www/delivery/spc.php?zones=";
31 /** Only fetch banners, that are really included in this page */
33 $('.oa').each(function() {
37 if (node.hasClass(name)) {
41 src += escape(id + '=' + zones[name] + "|");
46 src += "&nz=1&source=" + escape(OA_source);
47 src += "&r=" + Math.floor(Math.random()*99999999);
48 src += "&block=1&charset=UTF-8";
50 if (window.location) src += "&loc=" + escape(window.location);
51 if (document.referrer) src += "&referer=" + escape(document.referrer);
53 $.getScript(src, init_ads);
55 src = domain + '/www/delivery/fl.js';
63 for (i=0; i<queue.length; i++) {
65 if (typeof(OA_output[id]) != 'undefined' && OA_output[id] != '')
73 function render_ads() {
75 while (ads.length > 0) {
77 var result, src, inline, i;
84 // node.append(id + ": " + node.attr('class'));
87 * If output was added via document.write(), this output must be
88 * rendered before other banner-code from the OpenX-server is rendered!
90 if (output.length > 0) {
91 output.push(OA_output[id]);
93 for (i=0; i<output.length; i++)
94 OA_output[id] += output[i];
98 while ((result = /<script/i.exec(OA_output[id])) != null) {
99 node.append(OA_output[id].slice(0,result.index));
100 /** Strip all text before "<script" from OA_output[id] */
101 OA_output[id] = OA_output[id].slice(result.index,OA_output[id].length);
102 result = /^([^>]*)>([\s\S]*?)<\\?\/script>/i.exec(OA_output[id]);
103 if (result == null) {
104 /** Invalid syntax in delivered banner-code: ignoring the rest of this banner-code! */
105 // alert(OA_output[id]);
109 /** Remember iinline-code, if present */
112 /** Strip all text up to and including "</script>" from OA_output[id] */
113 OA_output[id] = OA_output[id].slice(result[0].length,OA_output[id].length);
114 result = /src\s*=\s*['"]([^'"]*)['"]/i.exec(src);
115 if (result == null) {
116 /** script-tag with inline-code: execute inline-code! */
117 result = /^\s*<.*$/m.exec(inline);
118 if (result != null) {
119 /** Remove leading HTML-comments, because IE will stumble otherwise */
120 inline = inline.slice(result[0].length,inline.length);
122 $.globalEval(inline);
125 /** script-tag with src-URL! */
126 ads.unshift(id); // << The banner might not be rendered fully, or include more calls to document.write().
127 /** Load the script and halt all work until the script is loaded and executed... */
128 $.getScript(result[1], render_ads); // << jQuery.getScript() generates onload-Handler for _all_ browsers ;)
134 node.append(OA_output[id]);
138 /** All entries from OA_output were rendered */
144 function document_write() {
146 for (var i=0; i<arguments.length; i++)
147 output.push(arguments[i]);
151 * Re-Add the last banner-code to the working-queue, because included
152 * scripts had added markup via document.write(), which is not
154 * Otherwise the added markup would be falsely rendered together with
155 * the markup from the following banner-code.
161 } ( window.openx = window.openx || {}, jQuery ));
163 var OA_output = {}; // << Needed, because IE will complain loudly otherwise!