Made a javascript program that accesses a random url but a problem! Help!

Cpus

Senior member
Apr 20, 2012
345
0
0
I made three different sections: tech, cars, and home. I made urls that corresspond to their topic in the script underneath each one. But whenever I click on the random link for any of the sections it only brings up the links from the last section. Please help.
 
Last edited:

sze5003

Lifer
Aug 18, 2012
14,304
675
126
You should use different variable names for the array functions. I would need to try it to see what it does but I'm not by a pc right now. Seems like it calls the same function randomlink() each time.
 

sze5003

Lifer
Aug 18, 2012
14,304
675
126
Try this:

<script>
<!--
/*
Random link button- By JavaScript Kit (http://javascriptkit.com)
Over 300+ free scripts!
This credit MUST stay intact for use
*/

//specify random links below. You can have as many as you want
var randomlinks=new Array()

randomlinks[0]="http://freewarejava.com"
randomlinks[1]="http://javascriptkit.com"
randomlinks[2]="http://dynamicdrive.com"
randomlinks[3]="http://cnn.com"
randomlinks[4]="http://www.geocities.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>

<!--Uncomment below to use a regular text link instead
<a href="javascript:randomlink()">Random Link</a>
-->
 
Last edited:

Cpus

Senior member
Apr 20, 2012
345
0
0
uwould i just put that under each section and change the urls accordingly?
 

sze5003

Lifer
Aug 18, 2012
14,304
675
126
Yeah just change the name of the function and create new functions for different sections.
 

Cpus

Senior member
Apr 20, 2012
345
0
0
so I could name it randomlink in one and randomji in another and randomg in another?
 

sze5003

Lifer
Aug 18, 2012
14,304
675
126
Your browser may be caching the results. Do a cntrl +f5. Also change the array names for example for each section: Tech: randomlinkTech[0], Home randomHome[0],1,2, so on. Auto: randomlinkAuto[0],1, 2, so on. For each function call do the same: randomlinkerHome(), randomlinkerAuto(), so on. And you only need the <script> </script> once on the page for all the javascript. Put all your javascript inside there.
 
Last edited:

sze5003

Lifer
Aug 18, 2012
14,304
675
126
That's odd it should work. Are you using ie or Firefox , chrome ? Try using chrom or Firefox it has a built in debugger.
 

Cpus

Senior member
Apr 20, 2012
345
0
0
doesnt work at all if I get rif of all but the beginning and end script or if I rename the RandomLink[0]
 

sze5003

Lifer
Aug 18, 2012
14,304
675
126
In the head section of the html document place your script tags there followed my the JavaScript inside there.
 

Cpus

Senior member
Apr 20, 2012
345
0
0
after head but before body? and javascript goes inside or outside body?
 

sze5003

Lifer
Aug 18, 2012
14,304
675
126
Each html document has a head section. Usually <head> </head>. The script tags with JavaScript go in there. On my phone on tapa I don't see any code..weird.
 

Dratickon

Junior Member
May 13, 2012
21
0
0
You've overwritten your array. Each time you click it's calling a function that pulls from the current randomlinks array. Since the last one has overwritten all of the previous ones, all of the functions are pulling from it. There are a couple of ways to fix it:

1. Create three separate arrays (three different variable names) or
2. Put the arrays within their respective functions

Code:
function randomlinker(){
[INDENT]
var randomlinks=new Array()

randomlinks[0]="http://allabouttech911.com"
randomlinks[1]="http://techradar.com"
randomlinks[2]="http://fudzilla.com"
randomlinks[3]="http://anandtech.com"
randomlinks[4]="http://xbitlabs.com"


window.location=randomlinks[Math.floor(Math.random()*randomlinks.length)]

[/INDENT]}
}
 

Cpus

Senior member
Apr 20, 2012
345
0
0
Am I still doing something wrong because I still cant fix it.
 
Last edited: