Click here to Skip to main content
15,905,616 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
there is a div tag this tag has background image and style.css has this image position etc.

how can I do this image will be change when every page refresh.I think I will use javascript but I didnt do

XML
<div id="background-container">
        <%--<img alt="" src="images/1.jpg">--%>

    </div>


this is div tag I try this

XML
<script  type="text/javascript">
        function getHelloThere() {
            document.getElementById('background-container').ChangeIt();
        }
</script>
  <script type="text/javascript">
            var totalCount = 6; I have image folder 6 image for background
            function ChangeIt() {
                var num = Math.ceil(Math.random() * totalCount);
                document.body.background = 'images/' + num + '.jpg';
}
   </script>


but nothing do
Posted

Try like this..


XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <script src="jquery-1.10.2.js" type="text/javascript"></script>
    <script type="text/javascript">
       var loading = function () {
            var totalCount = 6;
            var num = Math.floor(Math.random() * totalCount) + 1
            document.body.background = 'images/' + num + '.jpg'

        }
    </script>
</head>
<body onload=loading();>
    <form id="form1" runat="server">

    </form>
</body>
</html>
 
Share this answer
 
Comments
Deviprasad Das 20-Jan-14 4:10am    
This is a nice approach, ASP.NET also provides a control to achieve this.
You can use ADRotator control for doing this.
Karthik_Mahalingam 20-Jan-14 4:25am    
yes DPD, but answered as per OP's question :)
Karthik_Mahalingam 20-Jan-14 4:25am    
yes, but answered as per OP's question :)
Member-2338430 20-Jan-14 6:41am    
ok this is work KARTHIK Bangalore but image background repeated how to do no-repeat
Karthik_Mahalingam 20-Jan-14 7:10am    
you can check with css
http://www.w3schools.com/cssref/pr_background-repeat.asp
Try

XML
<html>
<head>
<script type="text/javascript">
var totalCount = 6;
function ChangeIt()
{
var num = Math.ceil( Math.random() * totalCount );
document.getElementById('background-container').style.background = 'images/'+num+'.jpg';

}
</script>
</head>
<body onload='ChangeIt()'>

</body>
</html>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900