CyberD.org
C:\ Home » Blog » Code » A Little Random Title Script

A Little Random Title Script

I made a little random title script for the movie review pages with more than one review. A little impulsive thing... that took 40 minutes to figure out. Ugh. Here's how it turned out though, in two parts. It's in JS.

First there's the header, that either shows a random title from a specific list, or if the user has JS disabled, a standard one:

<h1><script src="/theme/js/more-reviews.js" type="text/javascript"></script><noscript>More Reviews!</noscript></h1>

Then there's the script with the aforementioned list, and code to cycle through the titles randomly, above refereed to as more-reviews.js:

var r_text = new Array ();
r_text[0] = "More Reviews!";
r_text[1] = "Added Addages!";
r_text[2] = "Ongoing Opinions!";
r_text[3] = "Latter Ramblings!";
r_text[4] = "Future Views!";
r_text[5] = "Later Watches!";

var i = Math.floor(7*Math.random());

if (navigator.appName == "Netscape")
   {
   document.write(r_text[i]);
   }
else
   {
   document.write(r_text[i]);
   }

That's it! Depending on your number of titles you might need to change the formula below the list too. I tried using a solution that didn't rely on numbers at all first, but just couldn't get it working, so this is it.

This is what it looks like in action (reload page to see more):

It's such a simple thing, but I dug through such a huge mess of other methods before finding one that for some reason actually worked, that I felt like I needed to post about it. It's not all that strange this one did, but it's strange the others didn't. I still have no idea why. Something with the way H1 tags work? Something about including a JS script in a header? If you have any knowledge about such limitations I'd be intrigued to hear.

So, stranger who stumbled upon this post, if you're wise then feel free to send me a message, and if you're in the same situation - searching for a solution to the same problem, hopefully this will find you faster than it found me! And hopefully it'll work for you, too. It's just a little random random title script, but it sure took a big effort to get right.

Comments

Keep track of the discussion via rss? Read about comment etiquette? Or type in something below!
  1. S3C
    Sunday Dec/10/2017

    how do you run this and why bother create an if statement if both branches execute the same thing?

  2. Cyber
    Sunday Dec/10/2017

    You just embed it in any HTML page, wherever you want. Doesn't have to be in a header either. You could include the JS directly if you can't save it separately, it'd just get messy to have all that code in the middle of the page in my case. Makes it easier to edit when all instances refer to the same script, too. Better answer: https://www.quora.com/How-do-I-run-JavaScript-code

    If you ever want to just play around with code and test stuff right away: https://jsfiddle.net/

    As for the double statement hmm, man, I didn't notice that before. It's just a bit I've been using for all these things. It's possible it's some weird compatibility thing... will need to research!



The Comment Form

Your email address will not be published. Required fields are marked *

Question   Razz  Sad   Smile  Redface  Biggrin  Surprised  Eek   Confused   Cool  Mad   Twisted  Rolleyes   Wink  Idea  Neutral

Privacy   Copyright   Sitemap   Statistics   RSS Feed   Valid XHTML   Valid CSS   Standards

© CyberD.org 2017
Keeping the world since 2004.