2:40 AM

Javascript : Random Titles

posted under by FR3@K | Edit This
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">

<!--//

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

var num = 3;

var rantitle = new Array(num+1);

rantitle[1] = "This is random";

rantitle[2] = "Very Random";

rantitle[3] = "Yay it works";

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

document.title = rantitle[ranNumber];

}

//-->

</script>



Now to explain (Explanation has been color coded to match).

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.

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.

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

The code then draws a random number.

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

0 comments

Make A Comment
top