Randomizing Firefox start up homepage

I was told that the code below saved as an .html file would randomize the starting of firefox but I do not have the javascript know how to see what is wrong and wondered if someone could take a quick look and help me out?

<script type="text/javascript">
var aMyLink = new Array();
var nIndex;
nIndex = 0 + Math.floor(Math.random()*aMyLink.length);

Thanks :blush:

Nothing’s wrong with that code, it works well.

I would make some stylistic changes though, for which further details can be read about at https://github.com/airbnb/javascript

Those changes include:

[list][]replacing new Array() with []
]not using push, and instead defining the array contents as a group
[]use one var statement to declare variables
]assign nIndex when declaring the variable
[]remove the useless 0 +
]apply spacing between operators such as * and =

Which results in the following updated code:

var aMyLink = [
    nIndex = Math.floor(Math.random() * aMyLink.length);

window.location = aMyLink[nIndex];

but those changes are entirely for the benefit of the people reading the code. The web browser has no trouble running the original code, though it is admittedly slightly less efficient than the updated code.

We could then extract out different behaviour to separate functions:

function randomNumber(range) {
    return Math.floor(Math.random() * range);
function loadRandomPage(pages) {
    var pageNumber = randomNumber(pages.length);
    window.location = pages[pageNumber];

var pages = [


but once again, this is all for the benefit only of the people that read the code. It has no significant impact on the web browser that runs the code.

Most of such changes and improvements are in keeping with trying to make the code easier for the people that work with the code.

Hmmm. When I try it I get this in my browser:

<script type="text/javascript"> var aMyLink = new Array(); var nIndex; aMyLink.push('http://www.site1.com'); aMyLink.push('http://www.site2.co.uk'); aMyLink.push('http://www.site3.com'); nIndex = 0 + Math.floor(Math.random()*aMyLink.length); window.location=aMyLink [nIndex]; </script>

Any idea why? I save the code as an .html file and point homepage in firefox options to the .html files location

Something with your existing editor is converting the brackets to character entities.
Try using a simple editor instead, such as notepad. Then when you get things working, you can investigate further as to what in your editor is causing the problem.