Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    May 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    please help me with links...

    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.

  • #2
    Senior Coder coothead's Avatar
    Join Date
    Jan 2004
    Location
    chertsey, a small town 25 miles south west of london, england.
    Posts
    1,696
    Thanks
    0
    Thanked 232 Times in 227 Posts
    Hi there pretentious,

    and a warm welcome to these forums.

    Try this....
    Code:
    
    <!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
    Last edited by coothead; 05-22-2005 at 10:47 AM.

  • #3
    New to the CF scene
    Join Date
    May 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #4
    Supreme Master coder! _Aerospace_Eng_'s Avatar
    Join Date
    Dec 2004
    Location
    In a place far, far away...
    Posts
    19,291
    Thanks
    2
    Thanked 1,043 Times in 1,019 Posts
    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
    Code:
      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.
    Code:
    <script type="text/javascript" src="ran.js"></script>
    Can't get much clearer than that.

  • #5
    New to the CF scene
    Join Date
    May 2005
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 251 Times in 247 Posts
    Code:
     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.
    Code:
    a img {border:none}


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •