Random Ads | JavaScriptSource

Random Ads

Michael Stransky Jan 24, 2007


Do you want to display your own ads on your Web site? If you have a large number of them and only want to display a few at a time, this script will do it. They will display in random order, either horizontally or vertically.

The ads displayed below each have a link to the Web site of the advertiser and will change when the page is refreshed.


1. To show the ads vertically, change the script on the page to

<script type="text/javascript">
randomorder(adblock, '<br><br>')

2. To show the ads horizontally change the script on the page to

<script type="text/javascript">
randomorder(adblock, ' ')

3. The following portion of the code determines how many ads to display

var randomorder=new Array()
var the_one
var z=9
for (i=0;i<targetarray.length;i++)

The value of the "z" variable is very important. If you have 20 ads but only want to display 4 ads at a time, you would change the value of the "z" variable to "z=16" (without the quotes). This means that out of 20 ads, 16 will be hidden.

Source Code

Paste this source code into the designated areas.

External file

Paste this code into an external JavaScript file named: randomAds.js


Paste this code into your external CSS file or in the <style> section within the HEAD section of your HTML document.


Paste this code into the HEAD section of your HTML document.


Paste this code into the BODY section of your HTML document

Leave a Response


Thank you for this script. it was good

Chrisjoe Oct 21, 2016

Thank you so much for this simple and elegant script - it was just what I was looking for - you rock!

Kim May 29, 2008

This script works great. I've been to so many places to get something to work right and this works. I'm new to code writing only been doing for about 5 months and wrote my whole website so this is great! Thanks!

Dennis Peilecke Aug 17, 2007

You need to submit your question to the [a href="http://www.webdeveloper.com/forum/forumdisplay.php?f=3"]JavaScript Forum[/a].

Lee Jul 10, 2007

i don't know if it is possible to display 1 image at a time, would really like if it did and also be able to put it in different areas of my webpage. Would appreciate any help given.

Lanre Pedro Jun 20, 2007