Johannes Johannes - 1 year ago 53
HTML Question

Background image isn't changing

I'm trying to get a random background image. But when I load the page my background is empty.
This is my code:

function random_imglink(){
var myimages=new Array();
var count=Math.floor(Math.random() * myimages.length);
if (count==0){
document.write("'" + myimages[count] + "'");
<link rel="stylesheet" type="text/css" href="styles.css" />

<body background=random_imglink()>
<?php include ("header.php"); ?>
<div align="center" >
<?php include ("main.php"); ?>
<?php include ("footer.php"); ?>

I hope someone has got a solution to this problem?

Answer Source

Add this inside your script tag:

window.onload = function () {
    var imgs = [
    ]; = 'url(' + imgs[Math.round(Math.random() * (imgs.length - 1))] + ')';

How to set background in css

Don't forget to check if the images really exist.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download