Only delivered ads will be shown
[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   ads = [];
15
16
17   openx.show_ads = function(server, zones) {
18
19     var
20     domain = document.location.protocol == 'https:' ? 'https://' + server + ':8443':'http://' + server,
21     src = domain;
22
23     document.write = document_write;
24     document.writeln = document_write;
25
26     src += "/www/delivery/spc.php?zones=";
27
28     /** Only fetch banners, that are really included in this page */
29     $('.oa').each(function() {
30       var
31       node = $(this),
32       name, id;
33       for(name in zones) {
34         if (node.hasClass(name)) {
35           id = 'oa_' + ++count;
36           slots[id] = node;
37           src += escape(id + '=' + zones[name] + "|");
38         }
39       }
40     });
41
42     src += "&nz=1&source=" + escape(OA_source);
43     src += "&r=" + Math.floor(Math.random()*99999999);
44     src += "&block=1&charset=UTF-8";
45
46     if (window.location)   src += "&loc=" + escape(window.location);
47     if (document.referrer) src += "&referer=" + escape(document.referrer);
48
49     $.getScript(src, init_ads);
50
51     src = domain + '/www/delivery/fl.js';
52     $.getScript(src);
53
54   }
55
56   function init_ads() {
57
58     for (var id in slots) {
59       if (typeof(OA_output[id]) != 'undefined' && OA_output[id] != '')
60         ads.push(id);
61     }
62
63     render_ads();
64
65   }
66
67   function render_ads() {
68
69     while (ads.length > 0) {
70
71       id = ads.pop();
72       node = slots[id];
73
74       node.slideDown();
75
76       // node.append(id + ": " + node.attr('class'));
77
78       var result, src, inline;
79
80       while ((result = /<script/i.exec(OA_output[id])) != null) {
81         node.append(OA_output[id].slice(0,result.index));
82         /** Strip all text before "<script" from OA_output[id] */
83         OA_output[id] = OA_output[id].slice(result.index,OA_output[id].length);
84         result = /^([^>]*)>([\s\S]*?)<\\?\/script>/i.exec(OA_output[id]);
85         if (result == null) {
86           /** Invalid syntax in delivered banner-code: ignoring the rest of this banner-code! */
87           // alert(OA_output[id]);
88           OA_output[id] = "";
89         }
90         else {
91           /** Remember iinline-code, if present */
92           src = result[1]
93           inline = result[2];
94           /** Strip all text up to and including "</script>" from OA_output[id] */
95           OA_output[id] = OA_output[id].slice(result[0].length,OA_output[id].length);
96           result = /src\s*=\s*['"]([^'"]*)['"]/i.exec(src);
97           if (result == null) {
98             /** script-tag with inline-code: execute inline-code! */
99             result = /^\s*<.*$/m.exec(inline);
100             if (result != null) {
101               /** Remove leading HTML-comments, because IE will stumble otherwise */
102               inline = inline.slice(result[0].length,inline.length);
103             }
104             $.globalEval(inline);
105           }
106           else {
107             /** script-tag with src-URL! */
108             ads.push(id); // << The banner might not be rendered fully, or include more calls to document.write().
109             /** Load the script and halt all work until the script is loaded and executed... */
110             $.getScript(result[1], render_ads); // << jQuery.getScript() generates onload-Handler for _all_ browsers ;)
111             return;
112           }
113         }
114       }
115
116       node.append(OA_output[id]);
117       OA_output[id] = "";
118     }
119
120     /** All entries from OA_output were rendered */
121
122     id = undefined;
123     node = undefined;
124   }
125
126   function document_write() {
127
128     if (id == undefined)
129       return;
130
131     var
132     str = "",
133     i;
134
135     for (i=0; i < arguments.length; i++)
136       str += arguments[i];
137
138     OA_output[id] = str + OA_output[id];
139
140   }
141
142 } ( window.openx = window.openx || {}, jQuery ));
143
144 var OA_output = {}; // << Needed, because IE will complain loudly otherwise!