Random Text / Hyperlink Generator - Please Help

Hi Guys,

I’ve looked online and googled, but I just can’t find a simple solution to this one.

I am looking for some code/script that will allow me to generate random text / links on a page. The purpose? I have bought some advertising on a website in the form of a ‘text link’ and rather than send him new text everyday, I would rather he added some code which would generate it randomly, i.e.

  1. Click here for the latest sports news - BBC.com
  2. Latest Stocks and Shares information - HL.com

etc.

I can find scripts to generate random links, but not text also. Preferably this would be easy to implement and would work on all users browsers so I’m not sure if there is a best option to do this.

Thank you!

Hi there,

What do you mean by random text?

“Dog wheelbarrow handbag bang” is also random text, but probably useless to you in this case.

Do you have a set of phrases and you want to grab a random one of those and turn it into a link?

Hey

That’s correct - I have a set of text advertisements which I want to be randomly generated, and ideally I’d want each of them to go to individual links if possible.

Hi,

So you have (for example) text/url pairs:

Text => "Buy our cars"
URL => "www.audi.com"

Text => "Watch our news"
URL => "www.bbc.co.uk"

Text => "Feed your dog"
URL => "www.pedigreechum.com"

And you want to grab a random text/url pair and generate something like:

<a href="www.pedigreechum.com">Feed your dog</a>

Is that correct?

Yes absolutely spot on!

Ok then. This should do you:

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>Random ads, right to your screen</title>
  </head>
  
  <body>
    <h1>My great page</h1>
    <p id="adContainer"></p>

    <script>
      var adverts = [
        {
          text: "Buy our cars",
          url: "www.audi.com"
        },
        {
          text: "Watch our news",
          url: "www.bbc.co.uk"
        },
        {
          text: "Feed your dog",
          url: "www.pedigreechum.com"
        },
      ],
      adContainer = document.getElementById("adContainer"),
      advert = adverts[Math.floor(Math.random()*adverts.length)],
      link = document.createElement("a");

      link.setAttribute("href", advert.url);
      link.innerHTML = advert.text;
      adContainer.appendChild(link);
    </script>
  </body>
</html>

Thank you! I will try this and report back.

Hey

So I tried to use your code on this page - http://www.sescoops.com/jake-roberts-talks-rumble-hof-jbl-cole-show-wwe-msg-deals - but it did not work.

Any ideas?

Hi,

Sorry to hear that it didn’t work.

Yes. The page contains a bunch of errors that are nothing to do with the script I posted.
I would attempt to fix them first.

You can see them if you open the console whilst viewing the page.
How do I do that?

Thing is - it’s not my site.

Can you definitely verify that code you gave me works then at least I have something to go back with to prove it’s these other errors…

You can run the code I gave you on your own PC to verify it works for yourself.
All you do is create a new .txt file (assuming Windows), then change the ending to .html
After that open it with your favourite editor, then copy/paste my code into the new file.
Save it, then after that double click on your html file to open it in your browser.

Just tried it. The links did work, but sometimes when refreshing they were blank. I had to refresh again to get the link to come up.

Also got an error:

Webpage error details
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; MS-RTC LM 8)
Timestamp: Mon, 16 Dec 2013 11:31:31 UTC

Message: ‘advert.url’ is null or not an object
Line: 31
Char: 7
Code: 0
URI: file:///C:/Users/paajdi/Downloads/code.html

Is something like this achievable with html or php? I’m thinking a lot of people have JS disabled on their machines and it is an issue loading for some people given browsers, machines, etc.

Sorry, there was a comma too many in my original script.

You are using IE8 which then thinks that the array is 4 elements long, when in fact it is only 3.
Remove the comma and all will be good :slight_smile:

{
  text: "Feed your dog",
  url: "www.pedigreechum.com"
}, <---- Remove this

Sure, you could do the exact same thing with PHP, why not?
It probably even makes more sense, for the reasons you list.

<?php 
  $adverts = array(
    array("Buy our cars", "www.audi.com"),
    array("Watch our news", "www.bbc.co.uk"),
    array("Feed your dog", "www.pedigreechum.com")
  );

  $advert = $adverts[array_rand($adverts)];
  $ad = "<a href='" . $advert[1] . "'>" . $advert[0] . "</a>"
?>

<!DOCTYPE HTML>
<html>
  <head>
    <meta charset="utf-8">
    <title>Random ads, right to your screen</title>
  </head>
  
  <body>
    <h1>My great page</h1>
    <?php echo $ad ?>
  </body>
</html>

Thanks!!

I’ll try that one :slight_smile:

Sorry for the questions! I just saved the php code you gave me to a .html file and the links wouldn’t generate, is this because it needs to go onto a PHP file?

That’s correct.
You’ll need a server stack such as xampp to run PHP files locally.
If you have a bit of web space, just upload it there and it’ll work.