/** * adLoader * iwebshop的广告加载器 * * 先引入这个js,再load广告,1是广告位的id * * *
广告......
* *
* @author walu */ function adLoader() { var _self=this; var _id=0; var positionData={}; var adData={}; var urlPS=function(url) { url="/"+url+"/"; return url.replace(/[\/\\]+/g,"/"); } //获取广告位的信息,id广告位id this.getAdPositionInfo=function(id,async) { var url="/index.php?controller=site&action=ad_position_info"; var rand = Math.random(); var args = {'id':id,'rand':rand}; $.getJSON(url,args,function(c) { if(c['flag']==false) return; positionData=c['data']; //设置position的高和宽 var width,height; width=parseInt(positionData.width); height=parseInt(positionData.height); var element = $("#"+_self.elementID); element.css("overflow",'hidden'); if(width>0) { element.width(width); } if(height>0) { element.height(height); } _self.getAdList(id); }); } //获取某个广告位的广告列表,id:广告位id this.getAdList=function(id,async) { var url="/index.php?controller=site&action=ad_list"; var args = {'id':id,rand:Math.random()} $.getJSON(url,args,function(c) { if(c['flag']==false) return; adData=c['data']; _self.next(); }); } //加载广告 //id:广告位id //async:异步加载机制,如果为true则直接输出,否则等页面加载完成后再输出 this.load=function(id,elementID,async){ _self.id=id; _self.elementID=elementID; _self.async=async || false; _self.getAdPositionInfo( id,async ); } //广告位置div this.display=function(adDataKey) { _self.adDataKey=adDataKey; var ad=adData[adDataKey]; var width,height,code; width=(ad.width!=0) ?" width='"+ad.width+"' ":" "; height=(ad.height!=0) ?" height='"+ad.height+"' ":" "; switch( parseInt(ad['type']) ) { case 1: var code=""; code=code.replace(/{URL}/g,"/"+ad.content); break; case 2: var code=''; code=code.replace(/{URL}/g,"/"+ad.content); break; case 3: var code=ad.content; break; case 4: var code=ad.content; break; default: break; } e=document.getElementById(_self.elementID); //跳转事宜 if(ad.link!="#") { e.style.cursor="pointer"; e.onclick=function(){open(ad.link);}; } //异步与同步加载 if(_self.async==true) { $(document).ready(function(){ document.getElementById(_self.elementID).innerHTML=code; if(adData.length>1) setTimeout(function(){_self.next();},5000); }); } else { e.innerHTML=code; if(adData.length>1) setTimeout(function(){_self.next();},5000); } } this.next=function() { var adDataKey=_self.adDataKey || 0; var len=adData.length; if(positionData.fashion==1) { newKey=(adDataKey>=(len-1))?0:(adDataKey+1); } else { rand = parseInt(Math.random()*1000); newKey = rand%len; } _self.display(newKey); } }