From: Kai Moritz Date: Wed, 19 Sep 2012 11:00:50 +0000 (+0200) Subject: Script-Code is now loaded and executed via $.getScript() and $.globalEval() X-Git-Tag: 0.1.0~15 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=fda1b3a613cddb11351df6e1cb06c31229b3d073;p=openx Script-Code is now loaded and executed via $.getScript() and $.globalEval() The script now utilizes jQuery.getScript(url,callback) and jQuery.globalEval(code) when loading or executing the code of the detected script-tags, because these functions work around several issues in older browsers like IE 8 and Co. TODO: * The gloabal variable OA_output, in which the OpenX-scirpt spc places the fetched banner-codes, is not defined under IE8, when the callback is called after loading the script. Does the jQuery-function getScript() not wait for the script, like suggested by the function-name? --- diff --git a/openx.js b/openx.js index b916e78..92c25bc 100644 --- a/openx.js +++ b/openx.js @@ -4,8 +4,6 @@ (function( openx, $, undefined ) { - var body = document.getElementsByTagName('body')[0]; - var id; var node; @@ -21,14 +19,8 @@ var domain = document.location.protocol == 'https:' ? 'https://' + server + ':8443':'http://' + server; - var spc = document.createElement('script'); - - spc.type = 'text/javascript'; - spc.async = false; - spc.defer = false; - - spc.src = domain; - spc.src += "/www/delivery/spc.php?zones="; + var src = domain; + src += "/www/delivery/spc.php?zones="; /** Only fetch banners, that are really included in this page */ $('.oa').each(function() { @@ -37,32 +29,22 @@ if (node.hasClass(name)) { var id = 'oa_' + ++count; slots[id] = node; - spc.src += escape(id + '=' + zones[name] + "|"); + src += escape(id + '=' + zones[name] + "|"); } } }); - spc.src += "&nz=1&source=" + escape(OA_source); - spc.src += "&r=" + Math.floor(Math.random()*99999999); - spc.src += "&block=1&charset=UTF-8"; - - if (window.location) spc.src += "&loc=" + escape(window.location); - if (document.referrer) spc.src += "&referer=" + escape(document.referrer); - - spc.onload = init_ads; + src += "&nz=1&source=" + escape(OA_source); + src += "&r=" + Math.floor(Math.random()*99999999); + src += "&block=1&charset=UTF-8"; - body.appendChild(spc); + if (window.location) src += "&loc=" + escape(window.location); + if (document.referrer) src += "&referer=" + escape(document.referrer); + $.getScript(src, init_ads); - var fl = document.createElement('script'); - - fl.type = 'text/javascript'; - fl.async = false; - fl.defer = false; - - fl.src = domain + '/www/delivery/fl.js'; - - body.appendChild(fl); + src = domain + '/www/delivery/fl.js'; + $.getScript(src); } @@ -91,7 +73,6 @@ // node.append(id + ": " + node.attr('class')); var result; - var script; var src; var inline; @@ -115,20 +96,13 @@ result = /src\s*=\s*['"]([^'"]*)['"]/i.exec(src); if (result == null) { /** script-tag with inline-code: execute inline-code! */ - eval(inline); + $.globalEval(inline); } else { /** script-tag with src-URL! */ - script = document.createElement('script'); - script.type = 'text/javascript'; - script.async = false; - script.defer = false; - script.src = result[1]; - script.onload = render_ad; - /** The banner might not be rendered fully, or include more calls to document.write(). */ - ads.push(id); + ads.push(id); // << The banner might not be rendered fully, or include more calls to document.write(). /** Load the script and halt all work until the script is loaded and executed... */ - body.appendChild(script); // << The onload-event is only fired when appendChild is used! + $.getScript(result[1], render_ad); // << jQuery.getScript() generates onload-Handler for _all_ browsers ;) return; } }