php广告加载类用法实例

这篇文章主要介绍了php广告加载类用法实例,采用jQuery技术可实现异步与同步加载,具有非常广泛的实用价值,需要的朋友可以参考下

本文实例讲述了php广告加载类的用法,非常实用。分享给大家供大家参考。具体方法如下:

该php广告加载类,支持异步与同步加载。需要使用Jquery实现。

ADLoader.class.php类文件如下:

  1. <?php
  2. /** 广告加载管理类
  3. * Date: 2013-08-04
  4. * Author: fdipzone
  5. * Ver: 1.0
  6. *
  7. * Func:
  8. * public load 加载广告集合
  9. * public setConfig 广告配置
  10. * private getAds 根据channel创建广告集合
  11. * private genZoneId zoneid base64_encode 处理
  12. * private genHtml 生成广告html
  13. * private checkBrowser 检查是否需要同步加载的浏览器
  14. */
  15. class ADLoader{ // class start
  16. private static $_ads = array(); // 广告集合
  17. private static $_step = 300; // 广告加载间隔
  18. private static $_async = true; // 是否异步加载
  19. private static $_config = array(); // 广告设置文件
  20. private static $_jsclass = null; // 广告JS class
  21. /** 加载广告集合
  22. * @param String $channel 栏目,对应config文件
  23. * @param int $step 广告加载间隔
  24. * @param boolean $async 是否异步加载
  25. */
  26. public static function load($channel='', $step='', $async=''){
  27. if(isset($step) && is_numeric($step) && $step>0){
  28. self::$_step = $step;
  29. }
  30. if(isset($async) && is_bool($async)){
  31. self::$_async = $async;
  32. }
  33. // 判断浏览器,如IE强制使用同步加载
  34. if(!self::checkBrowser()){
  35. self::$_async = false;
  36. }
  37. self::getAds($channel);
  38. self::genZoneId();
  39. return self::genHtml();
  40. }
  41. /** 设置config
  42. * @param String $config 广告配置
  43. * @param String $jsclass js class文件路径
  44. */
  45. public static function setConfig($config=array(), $jsclass=''){
  46. self::$_config = $config;
  47. self::$_jsclass = $jsclass;
  48. }
  49. /** 根据channel创建广告集合
  50. * @param String $channel 栏目
  51. */
  52. private static function getAds($channel=''){
  53. $AD_Config = self::$_config;
  54. if($AD_Config!=null){
  55. self::$_ads = isset($AD_Config[$channel])? $AD_Config[$channel] : $AD_Config['default'];
  56. }
  57. }
  58. /** zoneid base64_encode 处理 */
  59. private static function genZoneId(){
  60. // 同步加载广告不需要处理zoneid
  61. if(!self::$_async){
  62. return ;
  63. }
  64. $ads = self::$_ads;
  65. for($i=0,$len=count($ads); $i<$len; $i++){
  66. if(isset($ads[$i]['zoneId'])){
  67. $ads[$i]['zoneId'] = base64_encode('var zonezoneId'].';');
  68. }
  69. }
  70. self::$_ads = $ads;
  71. }
  72. /** 生成广告html */
  73. private static function genHtml(){
  74. $ads = self::$_ads;
  75. $html = array();
  76. if(self::$_jsclass!=null && $ads){
  77. array_push($html, '<script type="text/javascript" src="'.self::$_jsclass.'"></script>');
  78. // 同步需要预先加载
  79. if(!self::$_async){
  80. foreach($ads as $ad){
  81. array_push($html, '<div .$ad['domId'].'_container" >');
  82. array_push($html, '<script type="text/javascript">');
  83. array_push($html, 'ADLoader.preload('.json_encode($ad).');');
  84. array_push($html, '</script>');
  85. array_push($html, '</div>');
  86. }
  87. }
  88. array_push($html, '<script type="text/javascript">');
  89. array_push($html, 'var ads='.json_encode($ads).';');
  90. array_push($html, '$(document).ready(function(){ ADLoader.load(ads, '.self::$_step.', '.intval(self::$_async).'); });');
  91. array_push($html, '</script>');
  92. }
  93. return implode("\r\n", $html);
  94. } //www.phpfensi.com
  95. /** 判断是否需要强制同步加载的浏览器 */
  96. private static function checkBrowser(){
  97. $user_agent = $_SERVER['HTTP_USER_AGENT'];
  98. if(strstr($user_agent,'MSIE')!=''){
  99. return false;
  100. }
  101. return true;
  102. }
  103. } // class end
  104. ?>

ADConfig.php文件如下:

  1. <?php
  2. /** 广告配置文件 **/
  3. return array(
  4. 'case_openx' => array(
  5. array(
  6. 'type' => 'openx',
  7. 'domId' => 'ad_728x90',
  8. 'zoneId' => 452
  9. ),
  10. array(
  11. 'type' => 'openx',
  12. 'domId' => 'ad_300x250',
  13. 'zoneId' => 449
  14. ),
  15. array(
  16. 'type' => 'openx',
  17. 'domId' => 'ad_l2_300x250',
  18. 'zoneId' => 394
  19. ),
  20. ),
  21. 'case_url' => array(
  22. array(
  23. 'type' => 'url',
  24. 'domId' => 'ad_728x90',
  25. 'url' => 'adurl.php?zone
  26. ),
  27. array(
  28. 'type' => 'url',
  29. 'domId' => 'ad_300x250',
  30. 'url' => 'adurl.php?zone
  31. ),
  32. array(
  33. 'type' => 'url',
  34. 'domId' => 'ad_l2_300x250',
  35. 'url' => 'adurl.php?zone
  36. )
  37. ),
  38. 'case_sync_openx' => array(
  39. array(
  40. 'type' => 'openx',
  41. 'domId' => 'ad_728x90',
  42. 'zoneId' => 452
  43. ),
  44. array(
  45. 'type' => 'openx',
  46. 'domId' => 'ad_300x250',
  47. 'zoneId' => 449
  48. ),
  49. array(
  50. 'type' => 'openx',
  51. 'domId' => 'ad_l2_300x250',
  52. 'zoneId' => 394
  53. ),
  54. ),
  55. 'default' => array(
  56. array(
  57. 'type' => 'openx',
  58. 'domId' => 'ad_728x90',
  59. 'zoneId' => 452
  60. ),
  61. array(
  62. 'type' => 'openx',
  63. 'domId' => 'ad_300x250',
  64. 'zoneId' => 449
  65. ),
  66. array(
  67. 'type' => 'openx',
  68. 'domId' => 'ad_l2_300x250',
  69. 'zoneId' => 394
  70. ),
  71. ),
  72. );
  73. ?>

ADLoader.js文件如下:

  1. /** 异步加载广告
  2. * Date: 2013-08-04
  3. * Author: fdipzone
  4. * Ver: 1.0
  5. */
  6. var ADLoader = (function(){
  7. var _ads = [], // 广告集合
  8. _step = 300, // 广告加载间隔
  9. _async = true, // 是否异步加载
  10. _loaded = 0; // 已经加载的广告数
  11. /** loadAd 循环加载广告
  12. * @param int c 第几个广告
  13. */
  14. function loadAD(c){
  15. if(_loaded>=_ads.length){
  16. return ;
  17. }
  18. if($('#'+_ads[c].domId).length>0){ // 判断dom是否存在
  19. if(_async){ // 异步执行
  20. crapLoader.loadScript(getScript(_ads[c]), _ads[c].domId, {
  21. success: function(){
  22. completeAd();
  23. }
  24. });
  25. }else{ // 将同步加载的广告显示
  26. var ad_container = $('#'+_ads[c].domId+'_container');
  27. ad_container.find('embed').attr('wmode','transparent').end().find('object').each(function(k, v){
  28. v.wmode = 'transparent'; // 将flash变透明
  29. });
  30. $('#'+_ads[c].domId)[0].appendChild(ad_container[0]);
  31. ad_container.show();
  32. completeAd();
  33. }
  34. }else{ // dom不存在
  35. completeAd();
  36. }
  37. }
  38. /** 加载完广告后处理 */
  39. function completeAd(){
  40. _loaded ++;
  41. setTimeout(function(){
  42. loadAD(_loaded);
  43. }, _step);
  44. }
  45. /** 获取广告
  46. * @param Array ad 广告参数
  47. */
  48. function getScript(ad){
  49. var ret = null;
  50. switch(ad.type){
  51. case 'openx': // openx code ad
  52. ret = 'data:text/javascript;base64,' + ad.zoneId + 'dmFyIG0zX3UgPSAobG9jYXRpb24ucHJvdG9jb2w9PSdodHRwczonPydodHRwczovL2Fkcy5ubWcuY29tLmhrL3d3dy9kZWxpdmVyeS9hanMucGhwJzonaHR0cDovL2Fkcy5ubWcuY29tLmhrL3d3dy9kZWxpdmVyeS9hanMucGhwJyk7CnZhciBtM19yID0gTWF0aC5mbG9vcihNYXRoLnJhbmRvbSgpKjk5OTk5OTk5OTk5KTsKaWYgKCFkb2N1bWVudC5NQVhfdXNlZCkgZG9jdW1lbnQuTUFYX3VzZWQgPSAnLCc7CmRvY3VtZW50LndyaXRlICgiPHNjciIrImlwdCB0eXBlPSd0ZXh0L2phdmFzY3JpcHQnIHNyYz0nIittM191KTsKZG9jdW1lbnQud3JpdGUgKCI/em9uZWlkPSIgKyB6b25laWQpOwpkb2N1bWVudC53cml0ZSAoJyZhbXA7Y2I9JyArIG0zX3IpOwppZiAoZG9jdW1lbnQuTUFYX3VzZWQgIT0gJywnKSBkb2N1bWVudC53cml0ZSAoIiZhbXA7ZXhjbHVkZT0iICsgZG9jdW1lbnQuTUFYX3VzZWQpOwpkb2N1bWVudC53cml0ZSAoZG9jdW1lbnQuY2hhcnNldCA/ICcmYW1wO2NoYXJzZXQ9Jytkb2N1bWVudC5jaGFyc2V0IDogKGRvY3VtZW50LmNoYXJhY3RlclNldCA/ICcmYW1wO2NoYXJzZXQ9Jytkb2N1bWVudC5jaGFyYWN0ZXJTZXQgOiAnJykpOwpkb2N1bWVudC53cml0ZSAoIiZhbXA7bG9jPSIgKyBlc2NhcGUod2luZG93LmxvY2F0aW9uKSk7CmlmIChkb2N1bWVudC5yZWZlcnJlcikgZG9jdW1lbnQud3JpdGUgKCImYW1wO3JlZmVyZXI9IiArIGVzY2FwZShkb2N1bWVudC5yZWZlcnJlcikpOwppZiAoZG9jdW1lbnQuY29udGV4dCkgZG9jdW1lbnQud3JpdGUgKCImY29udGV4dD0iICsgZXNjYXBlKGRvY3VtZW50LmNvbnRleHQpKTsKaWYgKGRvY3VtZW50Lm1tbV9mbykgZG9jdW1lbnQud3JpdGUgKCImYW1wO21tbV9mbz0xIik7CmRvY3VtZW50LndyaXRlICgiJz48XC9zY3IiKyJpcHQ+Iik7';
  53. break;
  54. case 'url': // url ad
  55. ret = ad.url;
  56. break;
  57. }
  58. return ret;
  59. }
  60. /** 同步加载广告
  61. * @param Array ad 广告参数
  62. */
  63. function writeAd(ad){
  64. switch(ad.type){
  65. case 'openx':
  66. var m3_u = (location.protocol=='https:'?'https://ads.nmg.com.hk/www/delivery/ajs.php':'http://ads.nmg.com.hk/www/delivery/ajs.php');
  67. var m3_r = Math.floor(Math.random()*99999999999);
  68. if (!document.MAX_used) document.MAX_used = ',';
  69. document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
  70. document.write ("?zoneid=" + ad.zoneId);
  71. document.write ('&cb=' + m3_r);
  72. if (document.MAX_used != ',') document.write ("&exclude=" + document.MAX_used);
  73. document.write (document.charset ? '&charset='+document.charset : (document.characterSet ? '&charset='+document.characterSet : ''));
  74. document.write ("&loc=" + escape(window.location));
  75. if (document.referrer) document.write ("&referer=" + escape(document.referrer));
  76. if (document.context) document.write ("&context=" + escape(document.context));
  77. if (document.mmm_fo) document.write ("&mmm_fo=1");
  78. document.write ("'><\/scr"+"ipt>");
  79. break;
  80. case 'url':
  81. document.write ('<script type="text/javascript" src="' + ad.url + '"></script>');
  82. break;
  83. }
  84. }
  85. obj = {
  86. /** 加载广告
  87. * @param Array ads 广告集合
  88. * @param int step 广告加载间隔
  89. * @param boolean async true:异步加载 false:同步加载
  90. */
  91. load: function(ads, step, async){
  92. _ads = ads;
  93. if(typeof(step)!='undefined'){
  94. _step = step;
  95. }
  96. if(typeof(async)!='undefined'){
  97. _async = async;
  98. }
  99. loadAD(_loaded);
  100. },
  101. /** 预加载广告 */
  102. preload: function(ad){
  103. if($('#'+ad.domId).length>0){ // 判断dom是否存在
  104. writeAd(ad);
  105. }
  106. }
  107. }
  108. return obj;
  109. }());
  110. /* crapLoader */
  111. var crapLoader = (function() {
  112. var isHijacked = false,
  113. queue = [],
  114. inputBuffer = [],
  115. writeBuffer = {},
  116. loading = 0,
  117. elementCache = {},
  118. returnedElements = [],
  119. splitScriptsRegex = /(<script[\s\S]*?<\/script>)/gim,
  120. globalOptions = {
  121. autoRelease: true,
  122. parallel: true,
  123. debug: false
  124. },
  125. defaultOptions = {
  126. charset: undefined,
  127. success: undefined,
  128. func: undefined,
  129. src: undefined,
  130. timeout: 3000
  131. },publ,
  132. head = document.getElementsByTagName("head")[0] || document.documentElement,
  133. support = {
  134. scriptOnloadTriggeredAccurately: false,
  135. splitWithCapturingParentheses: ("abc".split(/(b)/)[1]==="b")
  136. };
  137. function checkQueue () {
  138. if(queue.length) {
  139. loadScript( queue.shift() );
  140. } else if(loading === 0 && globalOptions.autoRelease) {
  141. debug("Queue is empty. Auto-releasing.");
  142. publ.release();
  143. }
  144. }
  145. function checkWriteBuffer (obj) {
  146. var buffer = writeBuffer[obj.domId],
  147. returnedEl;
  148. if(buffer && buffer.length) {
  149. writeHtml( buffer.shift(), obj );
  150. } else {
  151. while (returnedElements.length > 0) {
  152. returnedEl = returnedElements.pop();
  153. var id = returnedEl.id;
  154. var elInDoc = getElementById(id);
  155. if (!elInDoc) { continue; }
  156. var parent = elInDoc.parentNode;
  157. elInDoc.id = id + "__tmp";
  158. parent.insertBefore(returnedEl, elInDoc);
  159. parent.removeChild(elInDoc);
  160. }
  161. finished(obj);
  162. }
  163. }
  164. function debug (message, obj) {
  165. if(!globalOptions.debug || !window.console) { return; }
  166. var objExtra = "";
  167. if(obj) {
  168. objExtra = "#"+obj.domId+" ";
  169. var depth = obj.depth;
  170. while(depth--) { objExtra += " "; }
  171. }
  172. console.log("crapLoader " + objExtra + message);
  173. }
  174. function extend (t, s) {
  175. var k;
  176. if(!s) { return t; }
  177. for(k in s) {
  178. t[k] = s[k];
  179. }
  180. return t;
  181. }
  182. function finished (obj) {
  183. if(obj.success && typeof obj.success === "function") {
  184. obj.success.call( document.getElementById(obj.domId) );
  185. }
  186. checkQueue();
  187. }
  188. function flush (obj) {
  189. var domId = obj.domId,
  190. outputFromScript,
  191. htmlPartArray;
  192. outputFromScript = stripNoScript( inputBuffer.join("") );
  193. inputBuffer = [];
  194. htmlPartArray = separateScriptsFromHtml( outputFromScript );
  195. if(!writeBuffer[domId]) {
  196. writeBuffer[domId] = htmlPartArray;
  197. } else {
  198. Array.prototype.unshift.apply(writeBuffer[domId], htmlPartArray);
  199. }
  200. checkWriteBuffer(obj);
  201. }
  202. function getCachedElById (domId) {
  203. return elementCache[domId] || (elementCache[domId] = document.getElementById(domId));
  204. }
  205. function getElementById (domId) {
  206. return ( publ.orgGetElementById.call ?
  207. publ.orgGetElementById.call(document, domId) :
  208. publ.orgGetElementById(domId) );
  209. }
  210. function getElementByIdReplacement (domId) {
  211. var el = getElementById(domId),
  212. html, frag, div, found;
  213. function traverseForElById(domId, el) {
  214. var children = el.children, i, l, child;
  215. if(children && children.length) {
  216. for(i=0,l=children.length; i<l; i++) {
  217. child = children[i];
  218. if(child.id && child.id === domId) { return child; }
  219. if(child.children && child.children.length) {
  220. var tmp = traverseForElById(domId, child);
  221. if (tmp) return tmp;
  222. }
  223. }
  224. }
  225. }
  226. function searchForAlreadyReturnedEl(domId) {
  227. var i, l, returnedEl;
  228. for(i=0,l=returnedElements.length; i<l; i++) {
  229. returnedEl = returnedElements[i];
  230. if(returnedEl.id === domId) { return returnedEl; }
  231. }
  232. }
  233. if(el) { return el; }
  234. if (returnedElements.length) {
  235. found = searchForAlreadyReturnedEl(domId);
  236. if (found) {
  237. return found;
  238. }
  239. }
  240. if(inputBuffer.length) {
  241. html = inputBuffer.join("");
  242. frag = document.createDocumentFragment();
  243. div = document.createElement("div");
  244. div.innerHTML = html;
  245. frag.appendChild(div);
  246. found = traverseForElById(domId, div);
  247. if (found) {
  248. returnedElements.push(found);
  249. }
  250. return found;
  251. }
  252. }
  253. var globalEval = (function () {
  254. return (window.execScript ? function(code, language) {
  255. window.execScript(code, language || "JavaScript");
  256. } : function(code, language) {
  257. if(language && !/^javascript/i.test(language)) { return; }
  258. window.eval.call(window, code);
  259. });
  260. }());
  261. function isScript (html) {
  262. return html.toLowerCase().indexOf("<script") === 0;
  263. }
  264. function runFunc (obj) {
  265. obj.func();
  266. obj.depth++;
  267. flush(obj);
  268. }
  269. function loadScript (obj) {
  270. loading++;
  271. // async loading code from jQuery
  272. var script = document.createElement("script");
  273. if(obj.type) { script.type = obj.type; }
  274. if(obj.charset) { script.charset = obj.charset; }
  275. if(obj.language) { script.language = obj.language; }
  276. logScript(obj);
  277. var done = false;
  278. // Attach handlers for all browsers
  279. script.onload = script.onreadystatechange = function() {
  280. loading--;
  281. script.loaded = true;
  282. if ( !done && (!this.readyState ||
  283. this.readyState === "loaded" || this.readyState === "complete") ) {
  284. done = true;
  285. script.onload = script.onreadystatechange = null;
  286. debug("onload " + obj.src, obj);
  287. flush(obj);
  288. }
  289. };
  290. script.loaded = false;
  291. script.src = obj.src;
  292. obj.depth++;
  293. // Use insertBefore instead of appendChild to circumvent an IE6 bug.
  294. // This arises when a base node is used (#2709 and #4378).
  295. head.insertBefore( script, head.firstChild );
  296. setTimeout(function() {
  297. if(!script.loaded) { throw new Error("SCRIPT NOT LOADED: " + script.src); }
  298. }, obj.timeout);
  299. }
  300. function logScript (obj, code, lang) {
  301. debug((code ?
  302. "Inline " + lang + ": " + code.replace("\n", " ").substr(0, 30) + "..." :
  303. "Inject " + obj.src), obj);
  304. }
  305. function separateScriptsFromHtml (htmlStr) {
  306. return split(htmlStr, splitScriptsRegex);
  307. }
  308. function split (str, regexp) {
  309. var match, prevIndex=0, tmp, result = [], i, l;
  310. if(support.splitWithCapturingParentheses) {
  311. tmp = str.split(regexp);
  312. } else {
  313. // Cross browser split technique from Steven Levithan
  314. // http://blog.stevenlevithan.com/archives/cross-browser-split
  315. tmp = [];
  316. while(match = regexp.exec(str)) {
  317. if(match.index > prevIndex) {
  318. result.push(str.slice(prevIndex, match.index));
  319. }
  320. if(match.length > 1 && match.index < str.length) {
  321. Array.prototype.push.apply(tmp, match.slice(1));
  322. }
  323. prevIndex = regexp.lastIndex;
  324. }
  325. if(prevIndex < str.length) {
  326. tmp.push(str.slice(prevIndex));
  327. }
  328. }
  329. for(i=0, l=tmp.length; i<l; i=i+1) {
  330. if(tmp[i]!=="") { result.push(tmp[i]); }
  331. }
  332. return result;
  333. }
  334. function stripNoScript (html) {
  335. return html.replace(/<noscript>[\s\S]*?<\/noscript>/ig, "");
  336. }
  337. function trim (str) {
  338. if(!str) { return str; }
  339. return str.replace(/^\s*|\s*$/gi, "");
  340. }
  341. function writeHtml (html, obj) {
  342. if( isScript(html) ) {
  343. var dummy = document.createElement("div");
  344. dummy.innerHTML = "dummy<div>" + html + "</div>"; // trick for IE
  345. var script = dummy.children[0].children[0];
  346. var lang = script.getAttribute("language") || "javascript";
  347. if(script.src) {
  348. obj.src = script.src;
  349. obj.charset = script.charset;
  350. obj.language = lang;
  351. obj.type = script.type;
  352. loadScript(obj);
  353. } else {
  354. var code = trim( script.text );
  355. if(code) {
  356. logScript( obj, code, lang);
  357. globalEval( code, lang);
  358. }
  359. flush(obj);
  360. }
  361. } else {
  362. var container = getCachedElById(obj.domId);
  363. if(!container) {
  364. throw new Error("crapLoader: Unable to inject html. Element with id '" + obj.domId + "' does not exist");
  365. }
  366. html = trim(html); // newline before <object> cause weird effects in IE
  367. if(html) {
  368. container.innerHTML += html;
  369. }
  370. checkWriteBuffer(obj);
  371. }
  372. }
  373. function writeReplacement (str) {
  374. inputBuffer.push(str);
  375. debug("write: " + str);
  376. }
  377. function openReplacement () {
  378. // document.open() just returns the document when called from a blocking script:
  379. // http://www.whatwg.org/specs/web-apps/current-work/#dom-document-open
  380. return document;
  381. }
  382. function closeReplacement () {
  383. // document.close() does nothing when called from a blocking script:
  384. // http://www.whatwg.org/specs/web-apps/current-work/#dom-document-close
  385. }
  386. publ = {
  387. hijack: function(options) {
  388. if(isHijacked) { return; }
  389. isHijacked = true;
  390. extend(globalOptions, options);
  391. if(globalOptions.parallel && !support.scriptOnloadTriggeredAccurately) {
  392. globalOptions.parallel = false;
  393. debug("Browsers onload is not reliable. Disabling parallel loading.");
  394. }
  395. document.write = document.writeln = writeReplacement;
  396. document.open = openReplacement;
  397. document.close = closeReplacement;
  398. document.getElementById = getElementByIdReplacement;
  399. },
  400. release: function() {
  401. if(!isHijacked) { return; }
  402. isHijacked = false;
  403. document.write = this.orgWrite;
  404. document.writeln = this.orgWriteLn;
  405. document.open = this.orgOpen;
  406. document.close = this.orgClose;
  407. document.getElementById = this.orgGetElementById;
  408. elementCache = {};
  409. },
  410. handle: function(options) {
  411. if(!isHijacked) {
  412. debug("Not in hijacked mode. Auto-hijacking.");
  413. this.hijack();
  414. }
  415. var defaultOptsCopy = extend({}, defaultOptions);
  416. var obj = extend(defaultOptsCopy, options);
  417. obj.depth = 0;
  418. if (!obj.domId) {
  419. obj.domId = "craploader_" + new Date().getTime();
  420. var span = document.createElement("span");
  421. span.id = obj.domId;
  422. document.body.appendChild(span);
  423. }
  424. if (options.func) {
  425. runFunc(obj);
  426. return;
  427. }
  428. if(globalOptions.parallel) {
  429. setTimeout(function() {
  430. loadScript(obj);
  431. }, 1);
  432. } else {
  433. queue.push(obj);
  434. setTimeout(function() {
  435. if(loading === 0) {
  436. checkQueue();
  437. }
  438. }, 1);
  439. }
  440. },
  441. loadScript: function(src, domId, options) {
  442. if (typeof domId !== "string") {
  443. options = domId;
  444. domId = undefined;
  445. }
  446. this.handle(extend({
  447. src: src,
  448. domId: domId
  449. }, options));
  450. },
  451. runFunc: function(func, domId, options) {
  452. if (typeof domId !== "string") {
  453. options = domId;
  454. domId = undefined;
  455. }
  456. this.handle( extend({
  457. domId: domId,
  458. func: func
  459. }, options) );
  460. },
  461. orgGetElementById : document.getElementById,
  462. orgWrite : document.write,
  463. orgWriteLn : document.writeln,
  464. orgOpen : document.open,
  465. orgClose : document.close,
  466. _olt : 1,
  467. _oltCallback : function() {
  468. support.scriptOnloadTriggeredAccurately = (publ._olt===2);
  469. } //www.phpfensi.com
  470. };
  471. return publ;
  472. }());

demo.php示例程序如下:

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <head>
  4. <meta http-equiv="content-type" content="text/html; charset=utf-8">
  5. <title> AD Loader </title>
  6. <style type="text/css">
  7. .banner1{margin:10px; border:1px solid #CCCCCC; width:728px; height:90px;}
  8. .banner2{margin:10px; border:1px solid #CCCCCC; width:300px; height:250px;}
  9. </style>
  10. <script type="text/javascript" src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
  11. </head>
  12. <body>
  13. <div class="banner1" ></div>
  14. <div class="banner2" ></div>
  15. <div class="banner2" ></div>
  16. <?php
  17. function showAD($channel='', $step='', $async=''){
  18. include('ADLoader.class.php');
  19. $ad_config = include('ADConfig.php');
  20. ADLoader::setConfig($ad_config, 'ADLoader.js');
  21. return ADLoader::load($channel, $step, $async);
  22. }
  23. echo showAD('case_openx'); // 异步加载
  24. //echo showAD('case_url'); // url方式异步加载
  25. //echo showAD('case_sync_openx', 300, false); // 同步加载
  26. ?>
  27. </body>
  28. </html>

adurl.php文件如下:

  1. <?php
  2. $zoneid = isset($_GET['zoneid'])? intval($_GET['zoneid']) : 0;
  3. if($zoneid){
  4. ?>
  5. var zoneid = <?=$zoneid ?>;
  6. var m3_u = (location.protocol=='https:'?'https://ads.nmg.com.hk/www/delivery/ajs.php':'http://ads.nmg.com.hk/www/delivery/ajs.php');
  7. var m3_r = Math.floor(Math.random()*99999999999);
  8. if (!document.MAX_used) document.MAX_used = ',';
  9. document.write ("<scr"+"ipt type='text/javascript' src='"+m3_u);
  10. document.write ("?zoneid=" + zoneid);
  11. document.write ('&cb=' + m3_r);
  12. if (document.MAX_used != ',') document.write ("&exclude=" + document.MAX_used);
  13. document.write (document.charset ? '&charset='+document.charset : (document.characterSet ? '&charset='+document.characterSet : ''));
  14. document.write ("&loc=" + escape(window.location));
  15. if (document.referrer) document.write ("&referer=" + escape(document.referrer));
  16. if (document.context) document.write ("&context=" + escape(document.context));
  17. if (document.mmm_fo) document.write ("&mmm_fo=1");
  18. document.write ("'><\/scr"+"ipt>");
  19. <? } ?>