Networking | Hardware | Software | Multimedia | System | Unix&Linux | MBA

Home>>Hardware>>Random URL script help needed

Random URL script help needed

roger
11-23-2004, 09:08 PM
Hello,

I need some help with a javascript. I have made the below script to go to a random url. It works fine. But I need a thing added. I need to have a check on it which does a current url check. So it doesn't goes to the url im using the script on. But I want the domain on the list because I'm using it on more sites...which are listed in the array. So for the complete example I use it on domain1.com and when I click it checks first on what domain it is and then doesn't send it to that one in the list. Please help....Thanks Roger



<script>
<!--
var randomlinks=new Array()

randomlinks[0]="http://domain1.com"
randomlinks[1]="http://domain2.com"
randomlinks[2]="http://domain3.com"
randomlinks[3]="http://domain4.com"
randomlinks[4]="http://domain5.com"

function randomlink(){
window.location=randomlinks[Math.floor(Math.random()*randomlinks.length)]
}
//-->
</script>
<form>
<p><input type="button" name="B1" value="Random Link >>" onclick="randomlink()"></p> </form>

sage45
11-23-2004, 09:32 PM
Ok. So that I understand, you just don't want it to randomly pick the current url it is sitting on?

A follow-up question would be, do you want it to go back to that domain once it has left it? In other words you have four sites:

A
B
C
D

If you start on site A, you want your script to have A included in it, but not select it randomly. Now lets say the script chooses B as the next URL, when I arrived at B would you want the script to then be able to choose A?

-sage-

roger
11-23-2004, 09:43 PM
well what I want is that it doesn't goes to the url where it comes from, but I want to have the domain in the list because multiple domains are gonna use the list. It's like an webring idea. the best should be if I can set a sort of value that it remembers not to show the site it been on for the next 2 or 3 clicks. Hope you understand my idea.....Im Dutch so never mind the English :D
Thanks already,

Roger

Mr J
11-24-2004, 10:39 AM
Is the selected url going to replace the current page or are frames invloved?

If it is going to replace the current page then a cookie may have to be used or the query string

roger
11-24-2004, 02:11 PM
It supossed to change the current page....
Please can anyone help me...

Mr J
11-25-2004, 01:25 AM
The very first time the page is loaded a dynamic list of randomly selected urls is created from the main array (my_array) and the link set to go to the first url in the list.
When the page is reloaded the link is automatically set to go to the next url in this list
Once all the urls have been used a new dynamic list of urls is created.

A cookie is used to save the current list of randomly selected urls and the variable count, when the page is reloaded the current list is recreated, the variable count keeps track on which url is next and sets the link to suit.

Please try the following



<HTML>
<HEAD>
<TITLE>Document Title</TITLE>

<SCRIPT language=javascript>
<!--
// Realised by apachejeff
//www.huntingground.freeserve.co.uk

my_array=new Array()
my_array[0]="arc.htm"
my_array[1]="array.htm"
my_array[2]="clock.htm"
my_array[3]="url 4"
my_array[4]="url 5"
my_array[5]="url 6"
my_array[6]="url 7"
my_array[7]="url 8"
my_array[8]="url 9"
my_array[9]="url 10"


total_range=my_array.length // numbers range to select from
how_many=my_array.length // how many to select

function generate(){
total=total_range-1
oselect=how_many
numbers_range=new Array() // array to hold numbers to select from.
selected_num=new Array() // array to hold selected numbers

for(n=0;n<total+1;n++){ // create list of numbers to choose from
numbers_range[n]=n // start of numbers to choose from
}

for(p=0;p<oselect;p++){ // generate a random number for oselect times
rndnum=Math.round(Math.random()*(total))
selected_num[p]=numbers_range.splice(rndnum,1) // add to selected_num array and remove from numbers_range array
total--
}
show_selected_number()
}


count=0
function show_selected_number(){
if(count==selected_num.length){
count=0
generate()
return
}
document.getElementById("lnk1").href=my_array[selected_num[count]]
document.getElementById("lnk1").innerHTML=my_array[selected_num[count]]
count++
}


cookie_name="random_urls"
expdays=365

// An adaptation of Dorcht's cookie functions.

function set_cookie(name, value, expires, path, domain, secure){
if (!expires){expires = new Date()}
document.cookie = name + "=" + escape(value) +
((expires == null) ? "" : "; expires=" + expires.toGMTString()) +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
((secure == null) ? "" : "; secure");
}

function get_cookie(name) {
var arg = name + "=";
var alen = arg.length;
var clen = document.cookie.length;
var i = 0;
while (i < clen) {
var j = i + alen;
if (document.cookie.substring(i, j) == arg){
return get_cookie_val(j);
}
i = document.cookie.indexOf(" ", i) + 1;
if (i == 0) break;
}
return null;
}

function get_cookie_val(offset){
var endstr = document.cookie.indexOf (";", offset);
if (endstr == -1)
endstr = document.cookie.length;
return unescape(document.cookie.substring(offset, endstr));
}

function delete_cookie(name,path,domain){
document.cookie = name + "=" +
((path == null) ? "" : "; path=" + path) +
((domain == null) ? "" : "; domain=" + domain) +
"; expires=Thu, 01-Jan-00 00:00:01 GMT";
}

function save_data_to_cookie(){
var expdate = new Date ();
expdate.setTime (expdate.getTime() + (expdays*24*60*60*1000)); // expiry date
path=null // path="/" top directory, path=null this documents root directory
data=selected_num+","+count
set_cookie(cookie_name,data,expdate,path)
}

function get_cookie_data(){
inf=get_cookie(cookie_name)
if(!inf){
generate()
return
}
cookie_data=inf
cookie_data=cookie_data.split(",")
selected_num=new Array()
for(i=0;i<cookie_data.length-1;i++){
selected_num[i]=cookie_data[i]
}
count=cookie_data[cookie_data.length-1]
show_selected_number()
}

// -->
</SCRIPT>
</HEAD>
<BODY onload="get_cookie_data()" onunload="save_data_to_cookie()"><h1>Random urls</h1>

<center>
<a id="lnk1" href=http://www.codingforums.com/archive/index.php/"#null">Link</A>
</center>


</BODY>
</HTML>

roger
11-30-2004, 02:26 PM
Thanks Bro Thats Just What I Needed!!!!!


 

TOP

For more info

setting a minimum size
Auto Preview of Image 
Refresh with a query s
JS Validation obsolete
Javascript Pop up func
Slide show transitions
I'm a newbie...please!
how to write coding in
Advanced Debug Info 
.js file doesn't work 

News Archive

Adding Graphic To Text
re-direct form after s
pop up window controls
Clock on homepage - He
passing a param from a
Password Procection wi
document.title in same
focus onload 
Counting Wrong from Ri
Help needed on how to 

Related stories:

Copy link text to clipboard
Problem with htmlarea
Help with keystroke
possible to pass a form field name to a function, like this.value?
web forms proxy servers
List/menu link values
Need HELP on combo boxes

Copyright@2004-2005 www.zzcoke.com All Right Reserved

advanced web statistics