• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

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

Cpus

Senior member
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:
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.
 
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:
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:
That's odd it should work. Are you using ie or Firefox , chrome ? Try using chrom or Firefox it has a built in debugger.
 
doesnt work at all if I get rif of all but the beginning and end script or if I rename the RandomLink[0]
 
In the head section of the html document place your script tags there followed my the JavaScript inside there.
 
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.
 
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]}
}
 
Back
Top