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