Rotating banner using Javascript array

Posted under » JavaScript on 3 April 2014

PHP makes random or alternating banners a breeze. You can also do the same with Javascript.

Create the array

var mybanner = new Array();
mybanner[0] = "<a href=head>head</a>";
mybanner[1] = "<a href=shoulders>shoulders</a>";
mybanner[2] = "<a href=hands>hands</a>";

Randomnize or shuffle the array

var banner = mybanner[Math.floor(Math.random() * mybanner.length)];

Write it out.

document.write(banner + "<br>");


web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data