Random Page Titles with Javascript

Random Page Titles with Javascript

To have a random site title for you site you would place the following code between your HEAD tags.

<script type="text/javascript" language="javascript">
	
</script>

Now to explain. This script checks the browser type that the viewer is using and if it's a Netscape 4 or higher, it uses an IF statement to run the code.

	if (parseInt(navigator.appVersion) >= 4) {

Then the code deploys 2 variables. The first is "num" which is the Number of titles and MUST match the number of random titles being used. The second is "rantitle" which is an array that will form the random titles.

	var num = 3;
	var rantitle = new Array(num+1);

Next are the random titles labeled by a number (must never repeat).

	rantitle[1] = "This is random";
	rantitle[2] = "Very Random";
	rantitle[3] = "Yay it works";

The code then draws a random number.

	ranNumber = parseInt(num * Math.random() + 1);

After that the code finalizes by altering the title of the page with the title that matched the random number.

	document.title = rantitle[ranNumber];

Documentaion to Fire G. All rights © reserved. This may not be duplicated or reproduced without the consent of the owner.

Share the Love Share the Love

Add to Reddit Add to StumbleUpon Add to Mixx Add to Delicious Add to designfloat

About the author: Fire G

Hey, I'm the founder of Fire Studios, and thus, have my hands in everything that goes on here at FS. I manage the content, moderate the comments, design everything, code everything, provides a lot of articles, host the official podcast FS-Air, and run/manage most of the other sites in the FI family. Often times I'll come to be working on so many things that I hardly accomplish much, but that's what makes me who I am.

1 Reader Comment

  •  
  • Yeti
    September 3rd

    Great Tip! I think i may use it, its a cool feature! Thanks a lot!

Leave a Reply