在写一个项目时要用到广告模块,为了不想用ASP生成脚本代码时较麻烦,于是产生了写脚本类的念头,即是用一个类模块的脚本代码去控制所有在同一页面显示的漂浮广告。但在写的过程中发现JS脚本竟然在setTimeout里不能使用类的方法。奇怪,是不是我弄错了还是JS脚本就不能这样??但VBS脚本就可以!我晕……

不说了,贴代码:
[VBS脚本代码]



[JS脚本]




示例代码(包括两种脚本的使用):

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<HTML>

<HEAD>

<TITLE> 漂浮广告显示脚本类演示 </TITLE>

</HEAD>


<BODY>
<!--VBS脚本版的广告显示类示例程序-->
<script LANGUAGE="vbscript" src="Advervbs.vbs"></script>
<script LANGUAGE="vbscript">
Dim Adver,Adver1
Set Adver=New AdverClass
Adver.ObjName="Adver"
Adver.DivID=20
Adver.PrintHtml "img.gif","http://www.chinese.bj.cn/","这是什么"
Set Adver1=New AdverClass
Adver1.ObjName="Adver1"
Adver1.ImgType=1
Adver1.ImageWidth=299
Adver1.ImageHeight=87
Adver1.PrintHtml "​​​http://edu.qq.com/flash/moto-button.swf","http://www.chinese.bj.cn/'​​​ target="_blank" >​​http://edu.qq.com/flash/moto-button.swf","http://www.chinese.bj.cn/","这​​​是什么"
</script>
<!--JS脚本版的广告显示类示例程序-->
<script LANGUAGE="Javascript" src="AdverClass.js"></script>
<script LANGUAGE="Javascript">
var adver=new adverClass();
adver.setObjName("adver");
adver.setDivID(1);
adver.setImageType(1);
adver.setImagePX(299,87);
adver.showAdver("http://edu.qq.com/flash/moto-button.swf","http://www.chinese.bj.cn/' target="_blank" >​​​http://edu.qq.com/flash/moto-button.swf","http://www.chinese.bj.cn/","这​​​是什么");
var adver1=new adverClass();
adver1.showAdver("img.gif","http://www.chinese.bj.cn/","这是什么");
setTimer();
function setTimer(){
  adver.floatScroll();
  adver1.floatScroll();
  setTimeout("setTimer()",80);
}
</script>
</BODY>
</HTML>


以下是代码片段:

'/****************漂浮广告显示类****************************************************

'/* 作者:死在水中的鱼

'/* 脚本语言:VBS

'/* 用法: 

'/* Set Adver1=New AdverClass

'/* Adver1.ObjName="Adver1"   '设置当前的对象名  [本身对象变量名]

'/* Adver1.ImgType=1          '图片的类型    0=普通图片   1=Flash动画

'/* Adver1.ImageWidth=299     '图片的宽度

'/* Adver1.ImageHeight=87     '图片的高度

'/* ####以下方法显示广告图片(Flash) 对象.PrintHtml "图片地址","链接地址","提示信息"

'/* Adver1.PrintHtml "​​http://edu.qq.com/flash/moto-button.swf","http://www.chinese.bj.cn/'​​​ target="_blank" >​​http://edu.qq.com/flash/moto-button.swf","http://www.chinese.bj.cn/","这​​是什么"

'/***********************************************************************************

Class AdverClass

    Public DivID

 Public ObjName

 Private ObjDiv

 Public Delay    '改变数

 Public ImgType

 Private iStep

 Private iTop,iLeft,Width,Height

 Private TopFlag,LeftFlag

 '广告图片的大小

 Public ImageWidth,ImageHeight

 Private Sub Class_Initialize

     Randomize

     DivID=Int(Rnd(Time)*9999+1)

  Delay=80

  Height=document.body.clientHeight

  Width=document.body.clientWidth

  iTop=0

  iLeft=0

  TopFlag=False:LeftFlag=False

  iStep=3

  ImgType=0   '0 是图片  1 是FLASH文件

  ImageWidth=0

  ImageHeight=0

 End Sub

 Private Sub Class_Terminate

 End Sub

 Public Sub ScrollImg()

  Dim offHeight,offWidth,iRnd

  offWidth=ObjDiv.offsetWidth

  offHeight=ObjDiv.offsetHeight

  ObjDiv.style.left = iLeft + document.body.scrollLeft

  ObjDiv.style.top = iTop + document.body.scrollTop

  iRnd=Int(Rnd(Time)*99+1)

  If iRnd>97 Then TopFlag=Not TopFlag

  iRnd=Int(Rnd(Time)*9+1)

  If iRnd>98 Then LeftFlag=Not LeftFlag

  If TopFlag Then

     iTop=iTop+iStep*Rnd(Time)

  Else

     iTop=iTop-iStep*Rnd(Time)

  End If

  If LeftFlag Then

     iLeft=iLeft+iStep*Rnd(Time)

  Else

     iLeft=iLeft-iStep*Rnd(Time)

  End If

  If iTop<0 Then

     iTop=0

     TopFlag=True

  ElseIf iTop>Height-offHeight Then

     iTop=Height-offHeight

     TopFlag=False

  End If

  If iLeft<0 Then

     iLeft=0

     LeftFlag=True

  ElseIf iLeft>Width-offWidth Then

     iLeft=Width-offWidth

     LeftFlag=False

  End If

 End Sub

 Private Sub Start()

  setInterval ObjName&".ScrollImg()", Delay

 End Sub

 Public Sub PrintHtml(ByVal ImgSrc,ByVal adHref,ByVal adTitle)

        If ImgType=0 Then

     Call PrintImageHtml(ImgSrc,adHref,adTitle)

  Else

     Call PrintFlashHtml(ImgSrc,adHref,adTitle)

  End If

  Execute "Set ObjDiv=document.all.img"&DivID

  iLeft=Int(Rnd(Time)*(Width-100)+1)

  iTop=Int(Rnd(Time)*(Height-100)+1)

  ObjDiv.style.top=iTop

  ObjDiv.style.left=iLeft

  Call Start()

 End Sub

 Private Sub PrintImageHtml(ByVal ImgSrc,ByVal adHref,ByVal adTitle)

     If ImageWidth=0 Or Not IsNumeric(ImageWidth) Then

     ImageWidth=""

  Else

     ImageWidth=" width='"&ImageWidth&"'"

  End If

     If ImageHeight=0 Or Not IsNumeric(ImageHeight) Then

     ImageHeight=""

  Else

     ImageHeight=" height='"&ImageHeight&"'"

  End If

  document.write "<div id=""img"&DivID&""" style=""position:absolute;"">"

  document.write "<a href="""&adHref&""" target=""_blank"" title='"&adTitle&"'>"

  document.write "<img src="""&ImgSrc&""" alt="""&adTitle&""" border=""0"""&ImageWidth&ImageHeight&"></a></div>"

    End Sub

 Private Sub PrintFlashHtml(ByVal ImgSrc,ByVal adHref,ByVal adTitle)

     If ImageWidth=0 Or Not IsNumeric(ImageWidth) Then

     ImageWidth=80

  End If

  ImageWidth=" width='"&ImageWidth&"'"

     If ImageHeight=0 Or Not IsNumeric(ImageHeight) Then

     ImageHeight=80

  End If

  ImageHeight=" height='"&ImageHeight&"'"

  document.write "<div id=""img"&DivID&""" style=""position:absolute;"">"

  document.write "<a href="""&adHref&""" target=""_blank"" title='"&adTitle&"'>"

  document.write "<object  codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0'"&ImageWidth&ImageHeight&" align='middle'>"

  document.write "<param name='movie' value='"&ImgSrc&"'>"

  document.write "<param name='quality' value='high'>"

  document.write "<embed src='"&ImgSrc&"'"&ImageWidth&ImageHeight&" quality='high' pluginspage='http://www.macromedia.com/go/getflashplayer&#39; type='application/x-shockwave-flash'></embed></object>"

  document.write "</a></div>"

    End Sub

End Class