62,074
社区成员
发帖
与我相关
我的任务
分享
<script>
/*
Edit by ShowBo
--2008-5-11
please keep this message above,thks.
*/
function AdRorator(data){
this.data=data;
this.min=0;
this.max=(data&&data.length)?data.length-1:0;
this.adJson=null;
this.getRnd=function(min,max){
return Math.floor((max-min+1)*Math.random()+min);
}
this.getExt=function(Str){
try{
return /\.([^\.]+)$/.exec(Str)[1].toLocaleLowerCase();
}catch(e){return "";}
}
this.getLineHTML=function(){
return "<a href='"+this.adJson.naviUrl+"' target='"+(this.adJson.target?this.adJson.target:"_blank")+"' title='"+this.adJson.title+"'>"
}
this.getSizeHTML=function(){
return (this.adJson.width?" width='"+this.adJson.width+"px'":"")
+(this.adJson.height?" height='"+this.adJson.height+"px'":"");
}
this.getSwfHTML=function(){
return this.getLineHTML()
+'<embed src="'+this.adJson.imgUrl+'" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer"'
+' type="application/x-shockwave-flash"'+this.getSizeHTML()+'></embed></a>';
}
this.getImageHTML=function(){
return this.getLineHTML()
+"<img src='"+this.adJson.imgUrl+"' border='0' alt='" +this.adJson.title+"'"+this.getSizeHTML()+"/></a>";
}
this.getOtherHTML=function(){
return this.getLineHTML()+"点击查看\""+this.adJson.imgUrl+"\"</a>";
}
this.createHTML=function(){
if(this.max==this.min)return "";
this.adJson=this.data[this.getRnd(this.min,this.max)];
var adHTML="";
switch(this.getExt(this.adJson.imgUrl)){
case "swf":
adHTML+=this.getSwfHTML();
break;
case "gif":
case "jpg":
case "bmp":
case "png":
adHTML+=this.getImageHTML();
break;
default:
adHTML+=this.getOtherHTML();
break;
}
return adHTML;
}
}
//需要注意的是数据结构
var data=[{imgUrl:"http://b2b.lvyou168.cn/images/cttc.swf",naviUrl:"http://www.lvyou168.com",title:"中国旅游俱乐部",target:"_self",width:170,height:50}
,{imgUrl:"http://www.chinabn.org/cbntravel/images/logo.gif",naviUrl:"http://www.chinabn.org/cbntravel/",title:"中商世界游"}
,{imgUrl:"hello.txt",naviUrl:"http://www.chinabn.org",title:"中国商务集团"}];
var ad=new AdRorator(data);
document.write(ad.createHTML())
</script>