Chained loading of /delivery/spc.php and /delivery/fl.js
[openx] / openx.js
1 /** Optimized methods for fetching ad-banners via OpenX */
2
3 /** see: http://enterprisejquery.com/2010/10/how-good-c-habits-can-encourage-bad-javascript-habits-part-1/ */
4
5 (function( openx, $, undefined ) {
6
7   var
8
9   domain, id, node,
10
11   count = 0,
12   slots = {},
13   queue = [],
14   ads = [],
15   output = [];
16
17
18   openx.show_ads = function(server, zones) {
19
20     domain = document.location.protocol == 'https:' ? 'https://' + server + ':8443':'http://' + server;
21
22     var
23     name,
24     src = domain;
25
26     /**
27      * Without this option, jQuery appends an timestamp to every URL, that
28      * is fetched via $.getScript(). This can mess up badly written
29      * third-party-ad-scripts, that assume that the called URL's are not
30      * altered.
31      */
32     $.ajaxSetup({ cache: true });
33
34     src += "/www/delivery/spc.php?zones=";
35
36     /** Only fetch banners, that are really included in this page */
37     for(name in zones) {
38       $('.oa').each(function() {
39         var
40         node = $(this),
41         id;
42         if (node.hasClass(name)) {
43           id = 'oa_' + ++count;
44           slots[id] = node;
45           queue.push(id);
46           src += escape(id + '=' + zones[name] + "|");
47         }
48       });
49     }
50
51     if (typeof OA_source !== 'undefined')
52       src += "&source=" + escape(OA_source);
53     src += "&nz=1&r=" + Math.floor(Math.random()*99999999);
54     src += "&block=1&charset=UTF-8";
55
56     if (window.location)   src += "&loc=" + escape(window.location);
57     if (document.referrer) src += "&referer=" + escape(document.referrer);
58
59     $.getScript(src, load_flash);
60
61   }
62
63   function load_flash() {
64
65     $.getScript(domain + '/www/delivery/fl.js', init_ads);
66
67   }
68
69   function init_ads() {
70
71     var i, id;
72     for (i=0; i<queue.length; i++) {
73       id = queue[i];
74       if (typeof(OA_output[id]) != 'undefined' && OA_output[id] != '')
75         ads.push(id);
76     }
77
78     document.write = document_write;
79     document.writeln = document_write;
80
81     render_ads();
82
83   }
84
85   function render_ads() {
86
87     while (ads.length > 0) {
88
89       var result, src, inline, i;
90
91       id = ads.shift();
92       node = slots[id];
93
94       node.slideDown();
95
96       // node.append(id + ": " + node.attr('class'));
97
98       /**
99        * If output was added via document.write(), this output must be
100        * rendered before other banner-code from the OpenX-server is rendered!
101        */
102       insert_output();
103
104       while ((result = /<script/i.exec(OA_output[id])) != null) {
105         node.append(OA_output[id].slice(0,result.index));
106         /** Strip all text before "<script" from OA_output[id] */
107         OA_output[id] = OA_output[id].slice(result.index,OA_output[id].length);
108         result = /^([^>]*)>([\s\S]*?)<\\?\/script>/i.exec(OA_output[id]);
109         if (result == null) {
110           /** Invalid syntax in delivered banner-code: ignoring the rest of this banner-code! */
111           // alert(OA_output[id]);
112           OA_output[id] = "";
113         }
114         else {
115           /** Remember iinline-code, if present */
116           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!
117           inline = result[2];
118           /** Strip all text up to and including "</script>" from OA_output[id] */
119           OA_output[id] = OA_output[id].slice(result[0].length,OA_output[id].length);
120           result = /src\s*=\s*['"]?([^'"]*)['"]?\s/i.exec(src);
121           if (result == null) {
122             /** script-tag with inline-code: execute inline-code! */
123             result = /^\s*<.*$/m.exec(inline);
124             if (result != null) {
125               /** Remove leading HTML-comments, because IE will stumble otherwise */
126               inline = inline.slice(result[0].length,inline.length);
127             }
128             $.globalEval(inline);
129             insert_output(); // << The executed inline-code might have called document.write()!
130           }
131           else {
132             /** script-tag with src-URL! */
133             if (OA_output[id].length > 0)
134               /** The banner-code was not rendered completely yet! */
135               ads.unshift(id);
136             /** Load the script and halt all work until the script is loaded and executed... */
137             $.getScript(result[1], render_ads); // << jQuery.getScript() generates onload-Handler for _all_ browsers ;)
138             return;
139           }
140         }
141       }
142
143       node.append(OA_output[id]);
144       OA_output[id] = "";
145     }
146
147     /** All entries from OA_output were rendered */
148
149     id = undefined;
150     node = undefined;
151   }
152
153   /** This function is used to overwrite document.write and document.writeln */
154   function document_write() {
155
156     if (id == undefined)
157       return;
158
159     for (var i=0; i<arguments.length; i++)
160       output.push(arguments[i]);
161
162     if (id != ads[0])
163       /**
164        * Re-Add the last banner-code to the working-queue, because included
165        * scripts had added markup via document.write(), which is not
166        * proccessed yet.
167        * Otherwise the added markup would be falsely rendered together with
168        * the markup from the following banner-code.
169        */
170       ads.unshift(id);
171
172   }
173
174   /**
175    * This function prepends the collected output from calls to
176    * document_write() to the current banner-code.
177    */
178   function insert_output() {
179
180     if (output.length > 0) {
181       output.push(OA_output[id]);
182       OA_output[id] = "";
183       for (i=0; i<output.length; i++)
184         OA_output[id] += output[i];
185       output = [];
186     }
187
188   }
189
190 } ( window.openx = window.openx || {}, jQuery ));
191
192 var OA_output = {}; // << Needed, because IE will complain loudly otherwise!