当前位置: 首页 > 图文教程 > 网络编程 > Javascript > javascript 新浪背投广告实现代码

Javascript
javascript 新浪背投广告实现代码
javascript 选择文件夹对话框(web)
JS 自动完成 AutoComplete(Ajax 查询)
javascript scrollLeft,scrollWidth,clientWidth,offsetWidth 完全详解
jQuery 插件开发 其实很简单
最简单的jQuery程序 入门者学习
javascript 读取xml,写入xml 实现代码
JQuery 实现的页面滚动时浮动窗口控件
javascript 动态加载 css 方法总结
javascript 表格左右收缩
jqurey 学习笔记 传智博客佟老师附详细注释
javascript dom 操作详解 js加强
JS 动态添加列表框项效果代码
Javascript 事件流和事件绑定
Google Map Api和GOOGLE Search Api整合实现代码
9个javascript语法高亮插件 推荐
Prototype String对象 学习
javascript ImgBox透明遮罩层背景图片展示
javascript FAQ函数(提问+回复)
jQuery TextBox自动完成条

Javascript 中的 javascript 新浪背投广告实现代码


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 339 ::
收藏到网摘: n/a

新浪的好多广告代码被广大站长广为流传,大家如果有时间可以去看看,他们的代码都是写出类的,不少的好东西。 今天查看了下新浪的js代码,发现一个背投的广告类代码,不论是从学习角度还是使用角度都是不错的,但发现了一个问题,会被屏蔽的。
下面是具体的js类代码
复制代码 代码如下:

/*
轮播背投类 RotatorPB v3.1
Update by Dakular <[email protected]> 2008-8-25
格式:new RotatorPB(广告数组)
说明:第一次访问随机出现,以后访问顺序轮播;自动过滤过期广告;cookie时间24小时;商业广告数量不足时不显示
*/
if(typeof(RotatorPB)!='function'){
var RotatorPB=function (rad){
this.ary = new Array();
this.date = new Date();
this.w = rad.width;
this.h = rad.height;
this.num = rad.num;
this.o = rad.length;
this.id = RotatorPB.id++;
this.m = 'rpb_'+this.id;
this.n = new Array();
this.L = new Date();
this.e = 0;
var f;
var D = false;
var nn = 0;
//过滤无效广告
for(var i=0; i<rad.length; i++){
var start = RotatorPB.strToDate(rad[i][2].replace('<startdate>','').replace('</startdate>',''));
var end = RotatorPB.strToDate(rad[i][3].replace('<enddate>','').replace('</enddate>',''),true);
if(this.date>start && this.date<end && (this.num==null || this.ary.length<this.num) ){
this.ary.push([rad[i][0], rad[i][1], rad[i][4]]);
}
}
this.o = this.ary.length;
//取id
for(var i=0;i<this.o;i++){
f=this.m+'_'+(i+1);
g=RotatorPB.G(f);
if(g!=''){
this.n[i]=g;
D=true;
}else {
this.n[i]=0;
}
}
if(!D){
var r=Math.ceil(Math.random()*this.o);
var t=this.m+'_'+r;
RotatorPB.S(t,this.L.getTime(),1440);
this.e=r;
if(this.o==1){RotatorPB.S('s_dl',r,1440);}
//return r;
}else {
var R=this.n.join(',').split(',');
var k=R.sort();
var max=Number(k[k.length-1]);
var min=Number(k[0]);
var F;
for(var i=0;i<this.n.length;i++){
if(max==this.n[i]){
F=i+1;
break;
}
}
if(typeof(F)!='undefined'){
G=this.m+'_'+F;
H=Number(RotatorPB.G(G));
I=F%this.o+1;
J=this.m+'_'+I;
RotatorPB.S(J,this.L.getTime(),1440);
if(this.o==1){
I=-RotatorPB.G('s_dl');
if(I==0){I=1;RotatorPB.S('s_dl',1,1440);}
RotatorPB.S('s_dl',I,1440);
}
this.e=I;
//return I;
}
}
//Show AD
if(this.e==0 || this.ary.length==0) return; //如果没有广告则不显示
if(this.e==-1) return; //当只有一个广告时:始终显示第一个/奇数次刷新显示
var n = this.e-1;
var btsrc = this.ary[n][0];
var bturl = this.ary[n][1];
var bttype = btsrc.substring(btsrc.length-3).toLowerCase();
if(bttype=='.js'){ //js
document.write('<script language="javascript" type="text/javascript" src="'+btsrc+'"></scr'+'ipt>'); return;
}else if(bttype!='htm' && bttype!='tml'){
sinabturl = "http://d1.sina.com.cn/d1images/pb/pbv4.html?"+bturl+"${}"+bttype+"${}"+btsrc;
}else{
sinabturl = btsrc;
}
try{
aryADSeq.push("openWindowBack()");
}catch(e){
openWindowBack();
}
if(this.ary[n][2]!=""){ //监测计数
var oImg = new Image();
oImg.src = this.ary[n][2];
}
};
RotatorPB.id=1;
RotatorPB.G=function (N){
var c=document.cookie.split("; ");
for(var i=0;i<c.length;i++){
var d=c[i].split("=");
if(d[0]==N)return unescape(d[1]);
}return '';
};
RotatorPB.S=function (N,V,Q){
var L=new Date();
var z=new Date(L.getTime()+Q*60000);
document.cookie=N+"="+escape(V)+"; path=/; expires="+z.toGMTString()+";";
};
RotatorPB.strToDate = function(str,ext){
var arys = new Array();
arys = str.split('-');
var newDate = new Date(arys[0],arys[1]-1,arys[2],9,0,0);
if(ext){
newDate = new Date(newDate.getTime()+1000*60*60*24);
}
return newDate;
}
var openWindowBack = function(){
var popUpWin2 = open(sinabturl, (window.name!="popUpWin2")?"popUpWin2":"", "width=1,height=1,top=4000,left=3000");
}
};

具体的调用代码:
复制代码 代码如下:

<script src="rotator_pb.js"></script>
<SCRIPT language=javascript type=text/javascript>
//<![CDATA[
var rpb = new Array();
rpb.num = 2; //轮播数量
<!--2220E7B88D58-->//背投广告创意
rpb.push(["/upload/tech/20091012/20091012095940_c86a7ee3d8ef0b551ed58e354a836f2b.jpg", "http://www.ruanchen.com", "<startdate>2009-4-15</startdate>", "<enddate>2009-8-15</enddate>", ""]);
<!--$$ litong/2009-4-15 ~ 2009-8-15/B $-->
new RotatorPB(rpb);
//]]>
</SCRIPT>

上面的beitou.jpg是图片的地址,后面的是链接, 开始日期 结束日期什么的大家参考下就可以了。