How to change body background with image by javascript?

html body background change

Senior Developer Asked on January 23, 2017 in PHP.
Add Comment
1 Answer(s)

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

    <html>
    <head>
    <title>Change body background</title>
    <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;
    }
    </script>
    </head>
    <body>
    <img id="imgRotator" src="" alt="" height="470" width="600"/>
    </body>
    </html>
    
    Team Leader Answered on March 19, 2017.
    Add Comment

    Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.