一天弹出一次的广告JS代码
作者: 青锋幽灵 日期: 2008-09-10 02:02
有一个页面有两条图片广告,当用户访问这个页面时按十分之一的概率随机点击两个广告中的一个广告,当该用户已经弹出过广告则一天内不再弹出。
主函数:
name:cookie名称
urls:广告地址数组
width:宽度
height:高度
JavaScript代码
- function popad(name,urls,width,height)
- {
- if(!name || !urls) return false;
- width = width ? width : 300;
- height = height ? height : 300;
- var cookieString = new String(document.cookie);
- var cookieName = name+'=';
- var pos = cookieString.indexOf(cookieName);
- if (pos <0){
- if(rand(10)==1) {
- url = urls[rand(urls.length)-1];
- window.open(url,'hello','location=yes,resizable=yes,width='+width+',height='+height);
- var now = new Date();
- expiredate = new Date(now.getYear(),now.getMonth(),now.getDate(),23,59,59);
- document.cookie = name+'=opened;expires='+ expiredate.toGMTString();
- }
- }
- }
随机函数:
返回的值为1~number的值。
JavaScript代码
- function rand(number) {
- return Math.floor(Math.random() * number + 1);
- }
调用:
JavaScript代码
- adurls = new Array('http://www.163.com','http://www.cnfol.com');
- popad('popad', adurls, 800, 600);
发表评论
订阅
