Chapter 6.2-6.3
Chapter 6.2-6.3
***** For execution of code, store required images and web pages in the same folder where this code files are stored.
3. Pause before displaying the next banner advertisement: Set the timer.
setTimeout('DisplayBanners()',1000)
Program:
<html>
<head>
<title>Banner Ads</title>
<script type="text/javascript">
Banners = new Array('s1.jpg','s2.png','s3.png','s4.png'); // Declare image array
CurrentBanner = 0; //index variable
function DisplayBanners()
{
if (document.images)
{
CurrentBanner++;
if (CurrentBanner == Banners.length)
{
CurrentBanner = 0
}
document.RotateBanner.src= Banners[CurrentBanner]
setTimeout("DisplayBanners()",5000) // set timer
}
}
</script>
</head>
<body onload="DisplayBanners()"> // call to function
<center>
<img src="s1.jpg" width="400" height="275" name="RotateBanner" > // Setting banner
</center>
</body>
</html>
1. At the beginning of the JavaScript declare two arrays. First array is called as Banners that store url of
images(banner). Second array is called BannerLink. This array is initialized with strings that contain the URL for each
banner advertisement. It is critical that the URLs are in the same order as the banner images in the Banners array.
Banners = new Array('s1.jpg','s2.png','s3.png','s4.png');
BannerLink = new Array('page1.html','page2.html', 'page3.html','page4.html');
2. The LinkBanner() function definition inside <script> contains the statement that links the current banner to the
appropriate URL and then assigns the URL to the href attribute of the anchor tag on the web page. This statement uses
the index of the current banner as the index for the BannerLink array to identify the URL associated with the current
banner.
3. Use an anchor tag before <img> tag that displays banner. The href attribute of the anchor tag calls the LinkBanner()
function when the visitor clicks the banner.
<a href="javascript: LinkBanner()">
<img src="s4.png" width="400" height="275" name="RotateBanner" />
</a>
72
Program:
<html>
<head>
<title>Banner Ads</title>
<script type="text/javascript">
Banners = new Array('s1.jpg','s2.png','s3.png','s4.png');
BannerLink = new Array('page1.html','page2.html', 'page3.html','page4.html');
CurrentBanner = 0;
function LinkBanner()
{
document.location.href =BannerLink[CurrentBanner]
}
function DisplayBanners()
{
if (document.images)
{
CurrentBanner++;
if (CurrentBanner == Banners.length)
{
CurrentBanner = 0
}
document.RotateBanner.src= Banners[CurrentBanner]
setTimeout("DisplayBanners()",5000)
}
}
</script>
</head>
<body onload="DisplayBanners()" >
<center>
<a href="javascript: LinkBanner()">
<img src="s4.png" width="400" height="275" name="RotateBanner" >
</a>
</center>
</body>
</html>
4. Define RunSlideShow() function inside <script> tag. It accepts one parameter. If the value is 1,
then the value of the CurrentPicture is incremented, causing the next slide to be displayed. If the
value is –1, then the value of the CurrentPicture is decremented, caus-ing the previous slide to be
displayed. Before displaying the slide, we must determine whether the value of the CurrentPicture
variable is within the index range of the array. This is done by making sure that the value of the
CurrentPicture variable isn't greater than the last array element (Pictures.length – 1) and that value is
not less than the first array element (less than zero). If the value is beyond the range, then the value of
the CurrentPicture variable is reset to a valid index. The last step is to assign the proper array element
containing the slide to the src attribute of the tag, which is called PictureDisplay.
Program:
<html>
<head>
<title>Slideshow</title>
<script type="text/javascript">
Pictures = new Array("s1.jpg","s2.png","s3.png");
CurrentPicture = 0;
function RunSlideShow(i)
{
if (document.images)
{
if(CurrentPicture >Pictures.length-1)
CurrentPicture=0;
if(CurrentPicture<0)
CurrentPicture=Pictures.length-1;
document.PictureDisplay.src= Pictures[CurrentPicture];
CurrentPicture = CurrentPicture + i;
}
}
</script>
</head>
<body>
<p align="center"><img src="s4.png" name="PictureDisplay" width="400" height="275"/></p>
<center>
<table border="0">
<tr>
<td align="center">
<input type="button" value="Forward" onclick="RunSlideShow(1)">
<input type="button" value="Back" onclick="RunSlideShow(-1)">
</td>
</tr>
</table>
</center>
</body>
</html>