Script-Code is now loaded and executed via $.getScript() and $.globalEval()
[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 id;
8   var node;
9
10   var count = 0;
11   var slots = {};
12   var ads = [];
13
14
15   openx.show_ads = function(server, zones) {
16
17     document.write = render;
18     document.writeln = render;
19
20     var domain = document.location.protocol == 'https:' ? 'https://' + server + ':8443':'http://' + server;
21
22     var src = domain;
23     src += "/www/delivery/spc.php?zones=";
24
25     /** Only fetch banners, that are really included in this page */
26     $('.oa').each(function() {
27       var node = $(this);
28       for(var name in zones) {
29         if (node.hasClass(name)) {
30           var id = 'oa_' + ++count;
31           slots[id] = node;
32           src += escape(id + '=' + zones[name] + "|");
33         }
34       }
35     });
36
37     src += "&nz=1&source=" + escape(OA_source);
38     src += "&r=" + Math.floor(Math.random()*99999999);
39     src += "&block=1&charset=UTF-8";
40
41     if (window.location)   src += "&loc=" + escape(window.location);
42     if (document.referrer) src += "&referer=" + escape(document.referrer);
43
44     $.getScript(src, init_ads);
45
46     src = domain + '/www/delivery/fl.js';
47     $.getScript(src);
48
49   }
50
51   function init_ads() {
52
53     for (var id in slots) {
54       if (typeof(OA_output[id]) != 'undefined' && OA_output[id] != '')
55         ads.push(id);
56     }
57
58     render_ad();
59
60   }
61
62   function render_ad() {
63
64     if (ads.length == 0) {
65       id = undefined;
66       node = undefined;
67       return;
68     }
69
70     id = ads.pop();
71     node = slots[id];
72
73     // node.append(id + ": " + node.attr('class'));
74
75     var result;
76     var src;
77     var inline;
78
79     while ((result = /<script/i.exec(OA_output[id])) != null) {
80       node.append(OA_output[id].slice(0,result.index));
81       /** Strip all text before "<script" from OA_output[id] */
82       OA_output[id] = OA_output[id].slice(result.index,OA_output[id].length);
83       result = /^([^>]*)>([\s\S]*?)<\\?\/script>/i.exec(OA_output[id]);
84       if (result == null) {
85         /** Invalid syntax in delivered banner-code: ignoring the rest of this banner-code! */
86         // alert(OA_output[id]);
87         OA_output[id] = "";
88         render_ad();
89         return;
90       }
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         $.globalEval(inline);
100       }
101       else {
102         /** script-tag with src-URL! */
103         ads.push(id); // << The banner might not be rendered fully, or include more calls to document.write().
104         /** Load the script and halt all work until the script is loaded and executed... */
105         $.getScript(result[1], render_ad); // << jQuery.getScript() generates onload-Handler for _all_ browsers ;)
106         return;
107       }
108     }
109     node.append(OA_output[id]);
110     OA_output[id] = "";
111
112     /** This statement will only reached, when no script-element was rendered! */
113     render_ad();
114
115   }
116
117   function render() {
118
119     if (id == undefined)
120       return;
121
122     var str = "";
123     for (var i=0; i < arguments.length; i++)
124       str += arguments[i];
125
126     OA_output[id] = str + OA_output[id];
127
128   }
129
130 } ( window.openx = window.openx || {}, jQuery ));