Reputation: 3
How can I change an image once a week using JavaScript?
Upvotes: 0
Views: 527
Reputation: 89
<html>
<head>
<script type="text/javascript">
var arr=new Array();
arr=["0","1","2","3","4","5","6","7","8","9"];
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
function checkCookie()
{
var img=getCookie("imag");
if (img!=null && img!="")
{
document.getElementById("img").src=img+".jpg";
}
else
{
var rd=Math.random()*1000;
rd=String(rd);
rd=rd.substr(6,1);
setCookie("imag",rd,30);
}
}
</script>
</head>
<body onload="checkCookie()">
<img src="" id="img" height="87" />
</body>
</html>
set ten image in same folder with html file numberd as 1.jpg and 2.jpg ..... 9.jpg
Upvotes: 0
Reputation: 3523
What you could do is use Javascript to get the weeknumber and use that number to pick an appropriate image. If you want a different image for each week of the year, for example, you could have an image like image03.png for week 3.
However, I recomment doing this serverside to have the image already decided when delivering the webpage so the user doesn't experience a delay in having the image appear.
Upvotes: 1