Results 1 to 2 of 2

Thread: Show div based on today's date

  1. #1
    Join Date
    Jun 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Show div based on today's date

    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

    Code:
    <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>

  2. #2
    Join Date
    Jun 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    I like answering my own questions... If anyone is interested here's the solution:

    Code:
    <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.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •