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, _options, domain, id, node,
37 * Configuration-Options for jQuery.openx
39 * Since the domain-name of the ad-server is the only required parameter,
40 * jQuery.openx for convenience can be configured with only that one
41 * parameter. For example: "jQuery.openx('openx.example.org');". If more
42 * configuration-options are needed, they must be specified as an object.
43 * For example: "jQuery.openx({'server': 'openx.example.org', ... });".
48 * server: string Name of the server, without protocol or port. For
49 * example "openx.example.org". This option is
51 * protocol: Optional parameter.
52 * http: All connections to the ad-server are made via HTTP.
53 * https: All connections to the ad-server are made via HTTPS.
54 * If empty, document.location.protocol will be used.
55 * http_port: number Port-Number for HTTP-connections to the ad-server
56 * (only needed, when it is not the default-value 80).
57 * https_port: Port-Number for HTTPS-connections to the ad-server
58 * (only needed, when it is not the default-value 443).
61 * Seldom needed special Server-Settings (these parameters are only needed,
62 * if the default delivery-configration of the OpenX-Server was changed):
64 * path: string Path to delivery-scripts. DEFAULT: "/www/delivery".
65 * fl: string Flash-Include-Script. DEFAULT: "fl.js".
68 * Delivery-Options (for details and explanations see the see:
69 * http://www.openx.com/docs/2.8/userguide/single%20page%20call):
71 * block: 1 Don't show the banner again on the same page.
72 * 0 A Banner might be shown multiple times on the same
74 * blockcampaign: 1 Don't show a banner from the same campaign again on
76 * 0 A Banner from the same campaign might be shown
77 * muliple times on the same page (DEFAULT).
78 * target: string The value is addes as the HTML TARGET attribute in
79 * the ad code. Examples for sensible values: "_blank",
81 * withtext: 1 Show text below banner. Enter this text in the
82 * Banner properties page.
83 * 0 Ignore the text-field from the banner-properties
85 * charset: string Charset used, when delivering the banner-codes.
86 * If empty, the charset is guessed by OpenX. Examples
87 * for sensible values: "UTF-8", "ISO-8859-1".
92 * selector: string A selector for selecting the DOM-elements, that
93 * should display ad-banners. DEFAULT: ".oa".
94 * See: http://api.jquery.com/category/selectors/
96 $.openx = function( options ) {
100 console.error('jQuery.openx was already initialized!');
101 console.log('Configured options: ', _options);
106 /** Enable convenient-configuration */
107 if (typeof(options) == 'string')
108 options = { 'server': options };
112 if (!options.server) {
114 console.error('Required option "server" is missing!');
115 console.log('options: ', options);
122 'protocol': document.location.protocol,
123 'delivery': '/www/delivery',
131 domain = settings.protocol + '//';
132 domain += settings.server;
133 if (settings.protocol === 'http:' && settings.http_port)
134 domain += ':' + settings.http_port;
135 if (settings.protocol === 'https:' && settings.https_port)
136 domain += ':' + settings.https_port;
139 * Without this option, jQuery appends an timestamp to every URL, that
140 * is fetched via $.getScript(). This can mess up badly written
141 * third-party-ad-scripts, that assume that the called URL's are not
144 $.ajaxSetup({ 'cache': true });
147 * jQuery.openx only works with "named zones", because it does not know,
148 * which zones belong to which website. For mor informations about
150 * http://www.openx.com/docs/2.8/userguide/single%20page%20call
152 * For convenience, jQuery.openx only fetches banners, that are really
153 * included in the actual page. This way, you can configure jQuery.openx
154 * with all zones available for your website - for example in a central
155 * template - and does not have to worry about performance penalties due
156 * to unnecessarily fetched banners.
158 for(name in OA_zones) {
159 $(settings.selector).each(function() {
162 if (this.id === name) {
163 id = 'oa_' + ++count;
169 /** Fetch the JavaScript for Flash and schedule the initial fetch */
170 $.getScript(domain + settings.delivery + '/' + settings.fl, fetch_ads);
174 function fetch_ads() {
176 var name, src = domain + settings.delivery + '/spc.php';
178 /** Order banners for all zones that were found on the page */
182 src += escape(id + '=' + OA_zones[slots[id].id] + "|");
184 src += '&nz=1'; // << We want to fetch named zones!
187 * These are some additions to the URL of spc.php, that are originally
190 src += '&r=' + Math.floor(Math.random()*99999999);
191 if (window.location) src += "&loc=" + escape(window.location);
192 if (document.referrer) src += "&referer=" + escape(document.referrer);
194 /** Add the configured options */
195 if (settings.block === 1)
197 if (settings.blockcampaign === 1)
198 src += '&blockcampaign=1';
200 src += '&target=' + settings.target;
201 if (settings.withtext === 1)
202 src += '&withtext=1';
203 if (settings.charset)
204 src += '&charset=' + settings.charset;
206 /** Add the source-code - if present */
207 if (typeof OA_source !== 'undefined')
208 src += "&source=" + escape(OA_source);
210 /** Fetch data from OpenX and schedule the render-preparation */
211 $.getScript(src, init_ads);
215 function init_ads() {
218 for (i=0; i<queue.length; i++) {
220 if (typeof(OA_output[id]) != 'undefined' && OA_output[id] != '')
225 document.write = document_write;
226 document.writeln = document_write;
232 function render_ads() {
234 while (queue.length > 0) {
236 var result, src, inline;
243 // node.append(id + ": " + node.attr('class'));
246 * If output was added via document.write(), this output must be
247 * rendered before other banner-code from the OpenX-server is rendered!
251 while ((result = /<script/i.exec(OA_output[id])) != null) {
252 node.append(OA_output[id].slice(0,result.index));
253 /** Strip all text before "<script" from OA_output[id] */
254 OA_output[id] = OA_output[id].slice(result.index,OA_output[id].length);
255 result = /^([^>]*)>([\s\S]*?)<\\?\/script>/i.exec(OA_output[id]);
256 if (result == null) {
257 /** Invalid syntax in delivered banner-code: ignoring the rest of this banner-code! */
258 // alert(OA_output[id]);
262 /** Remember iinline-code, if present */
263 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!
265 /** Strip all text up to and including "</script>" from OA_output[id] */
266 OA_output[id] = OA_output[id].slice(result[0].length,OA_output[id].length);
267 result = /src\s*=\s*['"]?([^'"]*)['"]?\s/i.exec(src);
268 if (result == null) {
269 /** script-tag with inline-code: execute inline-code! */
270 result = /^\s*<.*$/m.exec(inline);
271 if (result != null) {
272 /** Remove leading HTML-comments, because IE will stumble otherwise */
273 inline = inline.slice(result[0].length,inline.length);
275 $.globalEval(inline);
276 insert_output(); // << The executed inline-code might have called document.write()!
279 /** script-tag with src-URL! */
280 if (OA_output[id].length > 0)
281 /** The banner-code was not rendered completely yet! */
283 /** Load the script and halt all work until the script is loaded and executed... */
284 $.getScript(result[1], render_ads); // << jQuery.getScript() generates onload-Handler for _all_ browsers ;)
290 node.append(OA_output[id]);
294 /** All entries from OA_output were rendered */
300 /** This function is used to overwrite document.write and document.writeln */
301 function document_write() {
306 for (var i=0; i<arguments.length; i++)
307 output.push(arguments[i]);
311 * Re-Add the last banner-code to the working-queue, because included
312 * scripts had added markup via document.write(), which is not
314 * Otherwise the added markup would be falsely rendered together with
315 * the markup from the following banner-code.
322 * This function prepends the collected output from calls to
323 * document_write() to the current banner-code.
325 function insert_output() {
327 if (output.length > 0) {
328 output.push(OA_output[id]);
330 for (i=0; i<output.length; i++)
331 OA_output[id] += output[i];
337 })( jQuery, window, document );
339 var OA_output = {}; // << Needed, because IE will complain loudly otherwise!