javascript 新浪背投廣告實現代碼

來源:互聯網
上載者:User

今天查看了下新浪的js代碼,發現一個背投的廣告類代碼,不論是從學習角度還是使用角度都是不錯的,但發現了一個問題,會被屏蔽的。
下面是具體的js類代碼 複製代碼 代碼如下:/*
輪播背投類 RotatorPB v3.1
Update by Dakular <shuhu@staff.sina.com.cn> 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(["http://img.jb51.net/demo/images/beitou.jpg", "http://www.jb51.net", "<startdate>2009-4-15</startdate>", "<enddate>2009-8-15</enddate>", ""]);
<!--$$ litong/2009-4-15 ~ 2009-8-15/B $-->
new RotatorPB(rpb);
//]]>
</SCRIPT>

上面的beitou.jpg是圖片的地址,後面的是連結, 開始日期 結束日期什麼的大家參考下就可以了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.