1 /** Optimized methods for fetching ad-banners via OpenX */
3 /** see: http://enterprisejquery.com/2010/10/how-good-c-habits-can-encourage-bad-javascript-habits-part-1/ */
5 (function( openx, $, undefined ) {
7 var body = document.getElementsByTagName('body')[0];
17 openx.show_ads = function(server, zones) {
19 document.write = render;
20 document.writeln = render;
22 var domain = document.location.protocol == 'https:' ? 'https://' + server + ':8443':'http://' + server;
24 var spc = document.createElement('script');
26 spc.type = 'text/javascript';
31 spc.src += "/www/delivery/spc.php?zones=";
33 /** Only fetch banners, that are really included in this page */
34 $('.oa').each(function() {
36 for(var name in zones) {
37 if (node.hasClass(name)) {
38 var id = 'oa_' + ++count;
40 spc.src += escape(id + '=' + zones[name] + "|");
45 spc.src += "&nz=1&source=" + escape(OA_source);
46 spc.src += "&r=" + Math.floor(Math.random()*99999999);
47 spc.src += "&block=1&charset=UTF-8";
49 if (window.location) spc.src += "&loc=" + escape(window.location);
50 if (document.referrer) spc.src += "&referer=" + escape(document.referrer);
52 spc.onload = init_ads;
54 body.appendChild(spc);
57 var fl = document.createElement('script');
59 fl.type = 'text/javascript';
63 fl.src = domain + '/www/delivery/fl.js';
71 for (var id in slots) {
72 if (typeof(OA_output[id]) != 'undefined' && OA_output[id] != '')
80 function render_ad() {
82 if (ads.length == 0) {
91 // node.append(id + ": " + node.attr('class'));
98 while ((result = /<script/i.exec(OA_output[id])) != null) {
99 node.append(OA_output[id].slice(0,result.index));
100 /** Strip all text before "<script" from OA_output[id] */
101 OA_output[id] = OA_output[id].slice(result.index,OA_output[id].length);
102 result = /^([^>]*)>([\s\S]*?)<\\?\/script>/i.exec(OA_output[id]);
103 if (result == null) {
104 /** Invalid syntax in delivered banner-code: ignoring the rest of this banner-code! */
105 // alert(OA_output[id]);
110 /** Remember iinline-code, if present */
113 /** Strip all text up to and including "</script>" from OA_output[id] */
114 OA_output[id] = OA_output[id].slice(result[0].length,OA_output[id].length);
115 result = /src\s*=\s*['"]([^'"]*)['"]/i.exec(src);
116 if (result == null) {
117 /** script-tag with inline-code: execute inline-code! */
121 /** script-tag with src-URL! */
122 script = document.createElement('script');
123 script.type = 'text/javascript';
124 script.async = false;
125 script.defer = false;
126 script.src = result[1];
127 script.onload = render_ad;
128 /** 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 body.appendChild(script); // << The onload-event is only fired when appendChild is used!
135 node.append(OA_output[id]);
138 /** This statement will only reached, when no script-element was rendered! */
149 for (var i=0; i < arguments.length; i++)
152 OA_output[id] = str + OA_output[id];
156 } ( window.openx = window.openx || {}, jQuery ));