Welcome to WebmasterWorld Guest from

Forum Moderators: open

Message Too Old, No Replies

random js ads?

random js ads?

8:01 pm on Jul 24, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:July 8, 2007
votes: 0

i have some ads codes
4 example adbrite YPN adsense clicksor

and this is a js file:

but how i can show them randomly?(i want show a ad in my site randomly - for example for first visit my blog show adbrite ads and for second visitor show clicksor ad...)

how i can do it?

9:06 pm on July 24, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:July 8, 2007
votes: 0

i have a blog on the blogspot
i puted my js file on the google pages ...
9:41 pm on July 24, 2007 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Apr 15, 2004
votes: 0

var ads = ['adbrite','YPN','ADSENSE','clicksor'];
var randInt = Math.floor(Math.random()*ads.length);

...develop fom there

6:34 am on July 25, 2007 (gmt 0)

Junior Member

10+ Year Member

joined:July 8, 2007
votes: 0

plz give me a full js file(an exam)
9:30 am on July 25, 2007 (gmt 0)

Preferred Member

10+ Year Member

joined:May 29, 2007
votes: 0

What follows is not as 'elegant' as the code Bernard Marx provided, but hopefully it will give you an idea of how arrays and the Math.random() function can work to display content 'at random'

Note: Arrays are ZERO based
The more you have, the more 'random' it will appear

<form name="myFormName" method="get" action=" ">
If you can see this then SCRIPTS are turned OFF
<script type="text/javascript">
function displayRandomAd()
//store the adverts in arrays
heading = new Array();
theAdText = new Array();

heading[0] = "heading ZERO";
theAdText[0] = "body ZERO Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec in purus a ,"
+ " dui venenatis laoreet. Integer faucibus convallis";

heading[1] = "heading ONE";
theAdText[1] = "The most overlooked advantage to owning a computer is that if they foul up there\'s no law"
+ " against whacking them around a little";

heading[2] = "John C. Dvorak";
theAdText[2] = "In all large corporation, there is a pervasive fear that someone, somewhere is having fun"
+ " with a computer on company time"
+ "<br\/>Networks help alleviate that fear";

heading[3] = "Douglas Adams (1952 ~ 2001) Mostly Harmless";
theAdText[3] = "A common mistake that people make when trying to design something competely foolproof "
+ " is to underestimate the ingenuity of compete fools";

heading[4] = "Rick Cook";
theAdText[4] = "Programming today is a race between software engineers striving to"
+ " build bigger and better idiot-proof programs, and the Universe trying to produce"
+ " bigger and better idiots"
+ "<br\/><br\/>So far, the Universe is winning";

heading[5] = "George W. Bush";
theAdText[5] = "Our enemies are innovative and resourceful, and so are we. "
+ "<br\/>They never stop thinking about new ways to harm our country and our people, and neither do we";

//calculate a random index
// NB the more ads you have, the less likely it is to see the same one twice in a row
index = Math.floor(Math.random() * theAdText.length);

//display the ad
document.write( ' <h2>Random Ad (refresh the page to see another ad)<\/h2>'
+ '<h4> ' + heading[index] + '<\/h4>'
+ '<p>' + theAdText[index] + '\n' + '<\/p>'
<script type="text/javascript">


Join The Conversation

Moderators and Top Contributors

Hot Threads This Week

Featured Threads

Free SEO Tools

Hire Expert Members