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