...

View Full Version : please help me with links...



pretentious
05-22-2005, 04:35 AM
I'm trying to make a random link thing on a lot of pages. But because I want them all to be the same and don't want to have to go and edit every page individually I'm trying to use something like:

<script language="javascript" src="gallery.js"></script>

and have the random link code in gallery.js. I've done this sort of thing for random pics but can't figure out how to do it with random links. I've tried a few different sites but they are all pretty much the same and nothing comes up.

How do you do this? Thanks,
Shane.

coothead
05-22-2005, 10:45 AM
Hi there pretentious,

and a warm welcome to these forums. :)

Try this....


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>random links</title>

<meta http-equiv="Content-Type" content="application/xhtml+xml; charset=utf-8" />

<style type="text/css">
/*<![CDATA[*/
/*//]]>*/
</style>

<script type="text/javascript">
//<![CDATA[

var urls=new Array();
urls[0]="http://www.codingforums.com";
urls[1]="http://www.w3schools.com";
urls[2]="http://www.google.com";
urls[3]="http://validator.w3.org/";

var i=Math.floor(Math.random()*urls.length);

function randomLinks() {

var elemA=document.createElement("a");
elemA.setAttribute("id","link"+i);
elemA.setAttribute("href",urls[i]);

document.getElementById('foo').appendChild(elemA);
document.getElementById('link'+i).innerHTML=urls[i];
}

//]]>
</script>

</head>
<body onload="randomLinks()">

<div id="foo"></div>

</body>
</html>


coothead

pretentious
05-23-2005, 06:45 AM
Yeah that's what I mainly get but I'm trying to do it a bit differently. Sorry to be hard. Anyway, because I want the same thing on lots of pages you know how you can put <script language="javascript" src="ran2.js"></script> on a page then have a file called ran2.js and have code in that. So then when you change ran2.js it changes on all the pages? Well I want the random link stuff to work in there, but it doesn't. How do I make it suitable to be put into ran2.js? Thanks for listening lol.

_Aerospace_Eng_
05-23-2005, 06:49 AM
Okay this isn't that hard, you are going to want the body onload there still. All you have to do is open up notepad, and put this inside it

var urls=new Array();
urls[0]="http://www.codingforums.com";
urls[1]="http://www.w3schools.com";
urls[2]="http://www.google.com";
urls[3]="http://validator.w3.org/";

var i=Math.floor(Math.random()*urls.length);

function randomLinks() {

var elemA=document.createElement("a");
elemA.setAttribute("id","link"+i);
elemA.setAttribute("href",urls[i]);

document.getElementById('foo').appendChild(elemA);
document.getElementById('link'+i).innerHTML=urls[i];
}
Save it as ran.js and then use this to put it on your page, it should go in your head tags.

<script type="text/javascript" src="ran.js"></script>
Can't get much clearer than that.

pretentious
05-23-2005, 09:30 AM
Okay engy old chap, I've done all that and I put <div id="foo"></div> on the page and the link shows up. But is there a way to make it a button or picture that goes to the link instead of the URL? Sorry to be a bother but I have no idea about any of this stuff. My basic coding knowledge extends to <center> lol.

glenngv
05-23-2005, 10:36 AM
var urls=new Array();
urls[0]=["http://www.codingforums.com", "image1.gif"];
urls[1]=["http://www.w3schools.com", "image2.gif"];
urls[2]=["http://www.google.com", "image3.gif"];
urls[3]=["http://validator.w3.org/", "image4.gif"];

var i=Math.floor(Math.random()*urls.length);

function randomLinks() {
var elemA=document.createElement("a");
elemA.setAttribute("id","link"+i);
elemA.setAttribute("href",urls[i][0]);

var img = document.createElement("img");
img.setAttribute("src",urls[i][1]);

elemA.appendChild(img);
document.getElementById('foo').appendChild(elemA);
}

You must have this on your CSS so that image links won't have border.
a img {border:none}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum