<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>js处理广告方案,可处理flash和图片</title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<body>
<div id="show" style="border:1px solid blue;margin:20px;"></div><br/>
<div id="show2" style="border:1px solid blue;margin:20px;"></div>
<div id="show1" style="border:1px solid red;margin:30px;height:120px;width:120px;"></div>
<script type="text/javascript">
<!--
/**广告操作类
用法:Advertisement.init("show","http://www.pcpp.com.cn/images/basic_qq/flash/fds111503.swf","http://www.baidu.com",'百度广告',600,500);
Advertisement.init("要显示区域的对象或id","flash或图片URL","点击的链接","显示文字","宽度","高度");
*/
Advertisement = {
width : 0, //宽度
height : 0, //高度
url : null, //flash或图片URL
targetUrl : null, //点击的链接地址
showInfo : "", //显示文字
init : function(toObj,url,targetUrl,text,w,h){ //初始化具体方法
this.width = w?w:200;
this.height = h?h:200;
this.url = url;
this.targetUrl = targetUrl;
this.showInfo = text;
toObj = (typeof toObj == "string")?this.$(toObj):toObj;
if(this.url.indexOf("swf")>-1){ //广告为flash
this.getStyle();
var adContain = this.createObj("div",{"className":"ad_contain"});
adContain.style.width = this.width+"px";
adContain.style.height = this.height+"px";
toObj.appendChild(adContain);
this.initFlash(adContain);
}else{ //广告为图片
this.initImg(toObj);
}
},$ : function(id){
return document.getElementById(id);
},createObj : function(tagName,config){ //创建一个对象,并初始化
var newObj = document.createElement(tagName);
for(var i in config){
eval("newObj."+i+"=config[i]");
}
return newObj
},write : function(info){ //在网页上输出信息
document.writeln(info);
},getStyle : function(){ //得到广告flash的样式
this.write("<style>");
this.write(".ad_contain{position:relative;display:block;overflow:hidden;background-color:#cccccc;}");
this.write(".ad_link{opacity:0; FILTER:alpha(opacity=0); position:absolute; left:0; top:0; cursor:pointer; z-index:10; display:inline; background-color:#fff;}");
this.write(".ad_flash{outline: medium none;}");
this.write("</style>");
},initFlash : function(toO){ //初始化flash显示
var flashObjStr = "<object data='"+this.url+"' id='ad_flash' style='width:"+this.width+"px;height:"+this.height+"px' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0'>";
flashObjStr += "<param name='Movie' VALUE='"+this.url+"'>";
flashObjStr += "<param name='Src' VALUE='"+this.url+"'>";
flashObjStr += "<param name='allowscriptaccess' value='always'>";
flashObjStr += "<param name='wmode' value='opaque'>";
flashObjStr += "<param name='quality' value='high'>";
flashObjStr += "<param name='wmode' value='transparent'/>";
flashObjStr += "<param name='type' value='application/x-shockwave-flash'>";
flashObjStr += "<embed src='"+this.url+"' wmode='transparent' quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer' type='application/x-shockwave-flash' width='"+this.width+"' height='"+this.height+"'></embed>";
flashObjStr += "</object>";
toO.innerHTML = flashObjStr;
toO.appendChild(this.getLink("ad_link"));
},initImg : function(toO){ //初始化图片显示
var linkObj = this.getLink();
toO.appendChild(linkObj);
var imgObj = this.createObj("img",{"style.border":"0","src":this.url,"width":this.width,"height":this.height});
linkObj.appendChild(imgObj);
},getLink : function(cN){ //得到一个超链接对象
var linkObject = this.createObj("a",{"className":(cN?cN:""),"target":"_blank","href":this.targetUrl});
this.showInfo?(linkObject.title = this.showInfo):"";
linkObject.style.width = this.width+"px";
linkObject.style.height = this.height+"px";
return linkObject;
}
};
//用法事例:
Advertisement.init("show",
"http://www.pcpp.com.cn/images/basic_qq/flash/fds111503.swf",
"http://www.yuegouok.com/product/S313F313D31303031T.html",'百度广告',
300,
250);
Advertisement.init(Advertisement.$("show2"),
"http://www.pcpp.com.cn/images/basic_qq/flash/fds111503.swf",
"http://www.yuegouok.com/product/S313F313D383239T.html",'百度广告11');
Advertisement.init("show1",
"http://pass.bccn.net/images/noavatar_middle.gif",
"http://www.yuegouok.com");
//-->
</script>
</body>
</html>