ionos

Monday, December 30, 2013

Change body background image using javascript

Put the java script between head tag.Create a folder "images" and keep all the images in that folder

HTML Code

img id="imgRotator" src="" alt="" height="470" width="600"/

Start Script

script type="text/javascript"
var picPaths = ['images/pic1.jpg','images/pic2.jpg','images/pic3.jpg','images/pic4.jpg'];
var oPics = [];
for(i=0; i < picPaths.length; i++){
oPics[i] = new Image();
oPics[i].src = picPaths[i];
}
curPic = Math.floor(Math.random()*oPics.length);
window.onload=function(){
document.getElementById('imgRotator').src = oPics[curPic].src;
}

End script


2 comments:

  1. Thanks Bikash, fore this script.
    I would recommend to use a ready made tool which allows you to upload unlimited images. Have a look at this Magento Background Images Extension by FME
    This extension changes the images automatically according to the settings done by the admin.

    ReplyDelete

Featured Post

Payza integration

Payza's Advanced Button in HTML Using simple HTML, you can integrate easily with Payza.Generate buttons and manage payment details f...

Most Popular