/    Sign up×
Community /Pin to ProfileBookmark

Random URL Refresh script

Im looking for a script that can refresh to a predefined list of URLs and each url has its own “weight”.

What I am trying to accomplish is this, a marketing campaign is going to be sending mass amounts of traffic. What I want is kind of load balancing between 2 URLS, so if when someone visits a URL, 10% of the time it will refresh to a similar URL.

I found

[CODE]<script type=”text/javascript”>
<!–
Array.prototype.random = function () {return this[Math.floor(Math.random() * this.length)]}

Date.ONE_SECOND = 1000;

url = [‘fee.html’, ‘fie.html’, ‘foe.html’, ‘fum.html’]

setTimeout(‘location = url.random()’, 5 * Date.ONE_SECOND)
// –>
</script>[/CODE]

But it’s not quite what I had in mind

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@rnd_meDec 03.2008 — if you want weight, simply repeat any urls you want to use more, and continue to apply the random pick.
Copy linkTweet thisAlerts:
@bmdmediaauthorDec 03.2008 — Ya I guess that could be one solution.

Since what Im going for is 10% to be balanced out, is should just do


url = ['url1.html', 'url1.html', 'url1.html', 'url1.html', 'url1.html', 'url1.html', 'url1.html', 'url1.html', 'url1.html', 'url2.html']


right?
Copy linkTweet thisAlerts:
@rnd_meDec 04.2008 — Ya I guess that could be one solution.

Since what Im going for is 10&#37; to be balanced out, is should just do ... right?[/QUOTE]


well, if that's the case, something a lot simpler would do:


[CODE]<script type="text/javascript"><!--

setTimeout(function(){
var num = parseInt(Math.random() * 10);
location.href = (num == 2 ? "foo.html" : "bar.html"); //loads bar 90%
}, 5000);

// --></script>[/CODE]


in english:

in 5 seconds from code loading:

generate random number between 0 and 9.

if that number is 2, goto foo.

otherwise, and usually, go to bar.
×

Success!

Help @bmdmedia spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...