PDA

View Full Version : Show div based on today's date



inko9nito
06-25-2007, 09:25 PM
I have this script that I got from some site. When you click on a link tied to a div it hides all other divs and displays just that one. Well, that works, but I want it to display one of the divs on page load based on today's date.

For example, it will display the "summer" div if today's date is anywhere between June 1 and August 31. When links to other divs are clicked that div will disappear and other ones will appear in its place. Anyway, here's the code, and I don't know anything about javascript so I have no idea where to even start editing it. Please help :(



<script type="text/javascript">
<!-- Layer Show/Hide Script
last=""
function changeDiv(the_div,the_change)
{
var the_style = getStyleObject(the_div);

if (the_style != false)
{
the_style.display = "block"

if(last!="")
{
last.display = "none"
}
}

last=the_style
}

function getStyleObject(objectId)
{
if (document.getElementById && document.getElementById(objectId))
{
return document.getElementById(objectId).style;
}

else if (document.all && document.all(objectId))
{
return document.all(objectId).style;
}

else
{
return false;
}
}

//-->
</script>

inko9nito
06-26-2007, 12:46 AM
I like answering my own questions... If anyone is interested here's the solution:


<script type="text/javascript">
function addLoadEvent(func)
{
var oldonload = window.onload;
if (typeof window.onload != 'function')
{
window.onload = func;
}

else
{
window.onload = function()
{
if (oldonload)
{
oldonload();
}
func();
}
}
}

addLoadEvent(function()
{
var today = new Date();
var month = today.getMonth()+1;

if(month >= 3 && month <=5)
changeDiv('spring');

else if(month >=6 && month <=8)
changeDiv('summer');

else if(month >= 9 && month <=11)
changeDiv('fall');

else if(month <=2 || month == 12)
changeDiv('winter');
})
</script>

I added 1 to "month" because it starts with 0 and it was easier to keep track of them if they started with 1.