2 * (C) Copyright 2012 juplo (http://juplo.de/).
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the GNU Lesser General Public License
6 * (LGPL) version 3.0 which accompanies this distribution, and is available at
7 * http://www.gnu.org/licenses/lgpl-3.0.html
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
19 * See http://coding.smashingmagazine.com/2011/10/11/essential-jquery-plugin-patterns/
20 * for detailed explanations for the applied best practices.
22 * The semicolon guides our code for poorly written concatenated scripts.
24 ;(function( $, window, document, undefined ) {
28 settings, _zones, _options, domain, id, node,
37 $.openx = function( zones, options ) {
41 console.error('jQuery.openx was already initialized!');
42 console.log('Configured zones: ', _zones);
43 console.log('Configured options: ', _options);
53 'protocol': document.location.protocol,
59 domain = settings.protocol + '//';
60 domain += settings.server;
61 if (settings.protocol === 'http:' && settings.http_port)
62 domain += ':' + settings.http_port;
63 if (settings.protocol === 'https:' && settings.https_port)
64 domain += ':' + settings.https_port;
71 * Without this option, jQuery appends an timestamp to every URL, that
72 * is fetched via $.getScript(). This can mess up badly written
73 * third-party-ad-scripts, that assume that the called URL's are not
76 $.ajaxSetup({ cache: true });
78 src += "/www/delivery/spc.php?zones=";
80 /** Only fetch banners, that are really included in this page */
82 $('.oa').each(function() {
86 if (node.hasClass(name)) {
90 src += escape(id + '=' + zones[name] + "|");
95 if (typeof OA_source !== 'undefined')
96 src += "&source=" + escape(OA_source);
97 src += "&nz=1&r=" + Math.floor(Math.random()*99999999);
98 src += "&block=1&charset=UTF-8";
100 if (window.location) src += "&loc=" + escape(window.location);
101 if (document.referrer) src += "&referer=" + escape(document.referrer);
103 $.getScript(src, load_flash);
107 function load_flash() {
109 $.getScript(domain + '/www/delivery/fl.js', init_ads);
113 function init_ads() {
116 for (i=0; i<queue.length; i++) {
118 if (typeof(OA_output[id]) != 'undefined' && OA_output[id] != '')
122 document.write = document_write;
123 document.writeln = document_write;
129 function render_ads() {
131 while (ads.length > 0) {
133 var result, src, inline;
140 // node.append(id + ": " + node.attr('class'));
143 * If output was added via document.write(), this output must be
144 * rendered before other banner-code from the OpenX-server is rendered!
148 while ((result = /<script/i.exec(OA_output[id])) != null) {
149 node.append(OA_output[id].slice(0,result.index));
150 /** Strip all text before "<script" from OA_output[id] */
151 OA_output[id] = OA_output[id].slice(result.index,OA_output[id].length);
152 result = /^([^>]*)>([\s\S]*?)<\\?\/script>/i.exec(OA_output[id]);
153 if (result == null) {
154 /** Invalid syntax in delivered banner-code: ignoring the rest of this banner-code! */
155 // alert(OA_output[id]);
159 /** Remember iinline-code, if present */
160 src = result[1] + ' ' // << simplifies the following regular expression: the string ends with a space in any case, so that the src-URL cannot be followed by the end of the string emediately!
162 /** Strip all text up to and including "</script>" from OA_output[id] */
163 OA_output[id] = OA_output[id].slice(result[0].length,OA_output[id].length);
164 result = /src\s*=\s*['"]?([^'"]*)['"]?\s/i.exec(src);
165 if (result == null) {
166 /** script-tag with inline-code: execute inline-code! */
167 result = /^\s*<.*$/m.exec(inline);
168 if (result != null) {
169 /** Remove leading HTML-comments, because IE will stumble otherwise */
170 inline = inline.slice(result[0].length,inline.length);
172 $.globalEval(inline);
173 insert_output(); // << The executed inline-code might have called document.write()!
176 /** script-tag with src-URL! */
177 if (OA_output[id].length > 0)
178 /** The banner-code was not rendered completely yet! */
180 /** Load the script and halt all work until the script is loaded and executed... */
181 $.getScript(result[1], render_ads); // << jQuery.getScript() generates onload-Handler for _all_ browsers ;)
187 node.append(OA_output[id]);
191 /** All entries from OA_output were rendered */
197 /** This function is used to overwrite document.write and document.writeln */
198 function document_write() {
203 for (var i=0; i<arguments.length; i++)
204 output.push(arguments[i]);
208 * Re-Add the last banner-code to the working-queue, because included
209 * scripts had added markup via document.write(), which is not
211 * Otherwise the added markup would be falsely rendered together with
212 * the markup from the following banner-code.
219 * This function prepends the collected output from calls to
220 * document_write() to the current banner-code.
222 function insert_output() {
224 if (output.length > 0) {
225 output.push(OA_output[id]);
227 for (i=0; i<output.length; i++)
228 OA_output[id] += output[i];
234 })( jQuery, window, document );
236 var OA_output = {}; // << Needed, because IE will complain loudly otherwise!