php 广告加载类(附源码)
本文导语: 本节内容: php 广告加载类,支持异步与同步加载。 需要使用Jquery类库。 1,广告加载类 ADLoader.class.php 代码示例: 2,配置文件类 ADConfig.php 代码示例: 3,ADLoader.js 代码示例: /** 异步加载广告 * Date: 2013-08...
本节内容:
php 广告加载类,支持异步与同步加载。
需要使用Jquery类库。
1,广告加载类 ADLoader.class.php
2,配置文件类 ADConfig.php
3,ADLoader.js
/** 异步加载广告
* Date: 2013-08-04
* Author: fdipzone
* Ver: 1.0
*/
var ADLoader = (function(){
var _ads = [], // 广告集合
_step = 300, // 广告加载间隔
_async = true, // 是否异步加载
_loaded = 0; // 已经加载的广告数
/** loadAd 循环加载广告
* @param int c 第几个广告
*/
function loadAD(c){
if(_loaded>=_ads.length){
return ;
}
if($('#'+_ads[c].domId).length>0){ // 判断dom是否存在
if(_async){ // 异步执行
crapLoader.loadScript(getScript(_ads[c]), _ads[c].domId, {
success: function(){
completeAd();
}
});
}else{ // 将同步加载的广告显示
var ad_container = $('#'+_ads[c].domId+'_container');
ad_container.find('embed').attr('wmode','transparent').end().find('object').each(function(k, v){
v.wmode = 'transparent'; // 将flash变透明
});
$('#'+_ads[c].domId)[0].appendChild(ad_container[0]);
ad_container.show();
completeAd();
}
}else{ // dom不存在
completeAd();
}
}
/** 加载完广告后处理 */
function completeAd(){
_loaded ++;
setTimeout(function(){
loadAD(_loaded);
}, _step);
}
/** 获取广告
* @param Array ad 广告参数
*/
function getScript(ad){
var ret = null;
switch(ad.type){
case 'openx': // openx code ad
ret = 'data:text/javascript;base64,' + ad.zoneId + 'dmFyIG0zX3UgPSAobG9jYXRpb24ucHJvdG9jb2w9PSdodHRwczonPydodHRwczovL2Fkcy5ubWcuY29tLmhrL3d3dy9kZWxpdmVyeS9hanMucGhwJzonaHR0cDovL2Fkcy5ubWcuY29
tLmhrL3d3dy9kZWxpdmVyeS9hanMucGhwJyk7CnZhciBtM19yID0gTWF0aC5mbG9vcihNYXRoLnJhbmRvbSgpKjk5OTk5OTk5OTk5KTsKaWYgKCFkb2N1bWVudC5NQVhfdXNlZCkg
ZG9jdW1lbnQuTUFYX3VzZWQgPSAnLCc7CmRvY3VtZW50LndyaXRlICgiPHNjciIrImlwdCB0eXBlPSd0ZXh0L2phdmFzY3JpcHQnIHNyYz0nIittM191KTsKZG9jdW1lbnQud3JpdGUgKCI/
em9uZWlkPSIgKyB6b25laWQpOwpkb2N1bWVudC53cml0ZSAoJyZhbXA7Y2I9JyArIG0zX3IpOwppZiAoZG9jdW1lbnQuTUFYX3VzZWQgIT0gJywnKSBkb2N1bWVudC53cml0ZSAoIiZhb
XA7ZXhjbHVkZT0iICsgZG9jdW1lbnQuTUFYX3VzZWQpOwpkb2N1bWVudC53cml0ZSAoZG9jdW1lbnQuY2hhcnNldCA/ICcmYW1wO2NoYXJzZXQ9Jytkb2N1bWVudC5jaGFyc2V0IDo
gKGRvY3VtZW50LmNoYXJhY3RlclNldCA/ICcmYW1wO2NoYXJzZXQ9Jytkb2N1bWVudC5jaGFyYWN0ZXJTZXQgOiAnJykpOwpkb2N1bWVudC53cml0ZSAoIiZhbXA7bG9jPSIgKyBlc2
NhcGUod2luZG93LmxvY2F0aW9uKSk7CmlmIChkb2N1bWVudC5yZWZlcnJlcikgZG9jdW1lbnQud3JpdGUgKCImYW1wO3JlZmVyZXI9IiArIGVzY2FwZShkb2N1bWVudC5yZWZlcnJlcikp
OwppZiAoZG9jdW1lbnQuY29udGV4dCkgZG9jdW1lbnQud3JpdGUgKCImY29udGV4dD0iICsgZXNjYXBlKGRvY3VtZW50LmNvbnRleHQpKTsKaWYgKGRvY3VtZW50Lm1tbV9mbykgZG
9jdW1lbnQud3JpdGUgKCImYW1wO21tbV9mbz0xIik7CmRvY3VtZW50LndyaXRlICgiJz48XC9zY3IiKyJpcHQ+Iik7';
break;
case 'url': // url ad
ret = ad.url;
break;
}
return ret;
}
/** 同步加载广告
* @param Array ad 广告参数
*/
function writeAd(ad){
switch(ad.type){
case 'openx':
var m3_u = (location.protocol=='https:'?'https://ads.nmg.com.hk/www/delivery/ajs.php':'http://ads.nmg.com.hk/www/delivery/ajs.php');
var m3_r = Math.floor(Math.random()*99999999999);
if (!document.MAX_used) document.MAX_used = ',';
document.write ("");
break;
case 'url':
document.write ('');
break;
}
}
obj = {
/** 加载广告
* @param Array ads 广告集合
* @param int step 广告加载间隔
* @param boolean async true:异步加载 false:同步加载
*/
load: function(ads, step, async){
_ads = ads;
if(typeof(step)!='undefined'){
_step = step;
}
if(typeof(async)!='undefined'){
_async = async;
}
loadAD(_loaded);
},
/** 预加载广告 */
preload: function(ad){
if($('#'+ad.domId).length>0){ // 判断dom是否存在
writeAd(ad);
}
}
}
return obj;
}());
/* crapLoader */
var crapLoader = (function() {
var isHijacked = false,
queue = [],
inputBuffer = [],
writeBuffer = {},
loading = 0,
elementCache = {},
returnedElements = [],
splitScriptsRegex = /( 0) {
returnedEl = returnedElements.pop();
var id = returnedEl.id;
var elInDoc = getElementById(id);
if (!elInDoc) { continue; }
var parent = elInDoc.parentNode;
elInDoc.id = id + "__tmp";
parent.insertBefore(returnedEl, elInDoc);
parent.removeChild(elInDoc);
}
finished(obj);
}
}
function debug (message, obj) {
if(!globalOptions.debug || !window.console) { return; }
var objExtra = "";
if(obj) {
objExtra = "#"+obj.domId+" ";
var depth = obj.depth;
while(depth--) { objExtra += " "; }
}
console.log("crapLoader " + objExtra + message);
}
function extend (t, s) {
var k;
if(!s) { return t; }
for(k in s) {
t[k] = s[k];
}
return t;
}
function finished (obj) {
if(obj.success && typeof obj.success === "function") {
obj.success.call( document.getElementById(obj.domId) );
}
checkQueue();
}
function flush (obj) {
var domId = obj.domId,
outputFromScript,
htmlPartArray;
outputFromScript = stripNoScript( inputBuffer.join("") );
inputBuffer = [];
htmlPartArray = separateScriptsFromHtml( outputFromScript );
if(!writeBuffer[domId]) {
writeBuffer[domId] = htmlPartArray;
} else {
Array.prototype.unshift.apply(writeBuffer[domId], htmlPartArray);
}
checkWriteBuffer(obj);
}
function getCachedElById (domId) {
return elementCache[domId] || (elementCache[domId] = document.getElementById(domId));
}
function getElementById (domId) {
return ( publ.orgGetElementById.call ?
publ.orgGetElementById.call(document, domId) :
publ.orgGetElementById(domId) );
}
function getElementByIdReplacement (domId) {
var el = getElementById(domId),
html, frag, div, found;
function traverseForElById(domId, el) {
var children = el.children, i, l, child;
if(children && children.length) {
for(i=0,l=children.length; i 1 && match.index < str.length) {
Array.prototype.push.apply(tmp, match.slice(1));
}
prevIndex = regexp.lastIndex;
}
if(prevIndex < str.length) {
tmp.push(str.slice(prevIndex));
}
}
for(i=0, l=tmp.length; i
5,广告代码 adurl.php
var zoneid = ;
var m3_u = (location.protocol=='https:'?'https://ads.nmg.com.hk/www/delivery/ajs.php':'http://ads.nmg.com.hk/www/delivery/ajs.php');
var m3_r = Math.floor(Math.random()*99999999999);
if (!document.MAX_used) document.MAX_used = ',';
document.write ("");
附,php 广告加载类源代码下载地址